DEV Community

Joni 【ジョニー】
Joni 【ジョニー】

Posted on • Originally published at Medium on

Consider using C# 8 with the .NET Framework

This post originally appeared on Medium

Consider using C# 8 with the .NET Framework

Yes, you read it right.

According to:

Building C# 8.0 | .NET Blog

… 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).

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?

After playing around with C# 8 for a while, I would definitely say, nullable reference types 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

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 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.

C# 8.0

Compile and then decompile it using dnSpy:

Decompiled code

Whoah! We have saved a bunch of keystrokes!

My favorite feature is “switch expressions.” Super impressed with it.

What is your favorite one?

Top comments (0)