Consider using C# 8 with the .NET Framework
Tags: csharp, dotnet
Published: 2019-11-04 09:47:22

---
title: Consider using C# 8 with the .NET Framework
published: true
date: 2019-10-31 22:01:03 UTC
tags: csharp,dotnet
canonical_url: https://medium.com/@joni2nja/consider-using-c-8-with-the-net-framework-9dceb20647c5
---
This post originally appeared on [Medium](https://medium.com/@joni2nja/consider-using-c-8-with-the-net-framework-9dceb20647c5)

### Consider using C# 8 with the .NET Framework

Yes, you read it right.

According to:

[Building C# 8.0 | .NET Blog](https://devblogs.microsoft.com/dotnet/building-c-8-0/)

> … using C# 8.0 is only supported on platforms that implement .NET Standard 2.1.

When we read it, our mental model would naturally say, okay, no support for the .NET Framework. Goodbye. Yes, we know that .NET Core is the future. But what if your superpower stubborn classic thinking customer dictated to use the now-dead-and-stuck (read: mature) .NET Framework technology?

A little good news for you. You can use some of the C# 8.0 features! 🎊

> **Yes, C# 8 can be used with the .NET Framework** and other targets older than .NET Core 3.0/.NET Standard 2.1 in Visual Studio 2019 (or older versions of Visual Studio if you [install a Nuget package](https://stackoverflow.com/a/58190585/397817)).

> The C# 8/.NET Framework combination is **not** officially supported by Microsoft. It is, they say, **for experts only**.

The C# 8/.NET Framework combination is not officially supported by Microsoft. It is, they say, for experts only.

More on this:

[Does C# 8 support the .NET Framework?](https://stackoverflow.com/questions/56651472/does-c-sharp-8-support-the-net-framework)

After playing around with C# 8 for a while, I would definitely say, [nullable reference types](https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references) are awesome! The compiler analyzes the flow of your code to see if a null value could make it to where you use it. It did catch my silly copy-pasted-and-then-forgot-to-edit bugs! Thanks!

Make sure to check the **C# 8.0 Feature Compatibility** table from Stuart Lang’s post as well:

[C# 8.0 and .NET Standard 2.0 - Doing Unsupported Things](https://dev.to/stuartblang/c-8-0-and-net-standard-2-0-doing-unsupported-things-3dk8-temp-slug-2870289)

Look at that. **_Support Category = 1 is Syntax Only — Language features that are purely syntax will just work!_**

Let’s prove it by adding [Directory.Build.props](https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build) file in the root directory so that every .csproj (project file) in the solution will inherit the settings:

```
<?xml version="1.0" encoding="utf-8"?>

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup>

    <LangVersion>latest</LangVersion>

    <Nullable>enable</Nullable>

  </PropertyGroup>

</Project>
```

and then write this crazy method.

![](https://cdn-images-1.medium.com/max/1024/1*ZxknVyCnzBRN3CQiMT49Pw.png)<figcaption>C# 8.0</figcaption>

Compile and then decompile it using [dnSpy](https://github.com/0xd4d/dnSpy):

![](https://cdn-images-1.medium.com/max/1024/1*0-Jlf9kxgiWXstsLKYmj9w.png)<figcaption>Decompiled code</figcaption>

Whoah! We have saved a bunch of keystrokes!

My favorite feature is [**“switch expressions.”**](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#switch-expressions) Super impressed with it.

What is your favorite one?