Special Offer: My C#/.NET Bootcamp Course is out now. Get 10% OFF using the code FRIENDS10.

.NET 5 is about to release in November, and with it comes the next version of the C# compiler. C# 9 offers many new features that developers have been waiting for for a long time. 

Let’s take a look at how to install and use C# 9 today. Make sure to stick with me until the end, where I show you one of the new C# 9 features.

First, let’s take a look at the steps we need to take to make C# 9 working on our system. There is an article in the Microsoft Docs about C# language versioning that helps us a lot.

There is a lot of interesting information about what C# version runs on which target framework. 

In this video, we focus on making C# 9 work. The last paragraph in this article tells us that C# 9 is supported only on .NET 5 and newer versions.

Wait, at the time of recording this video .NET 5 isn’t released yet. Does it mean we cannot use C# 9? Well, yes and no. Microsoft released .NET 5 preview versions and, most recently, .NET 5 release candidates.

Download and Install .NET 5

Let’s head over to the .NET website and download .NET 5.

If you’re watching this video when .NET 5 is released, download it and install it on your computer. If you’re watching it before the release, download the latest release candidate. 

At the time of recording this video, RC 2 is the most recent version. The most current version is always the first version on the website.

Installing or Updating Visual Studio 2019

The next step is to make sure we have a current installation of Visual Studio 2019. To use C# 9, we need at least version 16.7. To be able to use all the .NET 5 features, we would need at least 16.8.

For many people updating the regular Visual Studio version worked to use C# 9. For me, it only worked with the preview version of Visual Studio. After thinking about it, I would suggest using the Visual Studio preview track to explore new features. As soon as .NET 5 is released, it should be working on the regular Visual Studio version.

The preview version’s idea is to be able to run the latest tools and SDKs. Another benefit is that we do not touch the stable Visual Studio version that we might use for work or other projects. Both instances run side-by-side without interfering with each other.

If you are on an older version, you need to update to at least 16.7. I would generally suggest to always update to the latest Visual Studio version. 

The worst thing we as developers want is to get stuck because of a bug in our IDE that has already been fixed in a more recent version. Don’t step into that trap, and always keep your IDE on a current version. A lumberjack wouldn’t use a dull saw; why should we, as developers, use outdated tools?

That was a long talk about Visual Studio versions. The critical takeaway is to use the preview version for preview features and the stable version for released SDKs and C# versions.

Configuring Visual Studio to Allow Previews Versions

In the next step, we enable the support of the .NET Core SDK preview versions in Visual Studio. Within Visual Studio, open the Tools menu, click on options, and select the Preview Features item in the Environment group.

There are a couple of preview features that can be enabled if we need them. To make C# 9 available, we need to enable the Use previews of the .NET Core SDK option.

Remember to restart Visual Studio after enabling the preview feature. Visual Studio tells us that a restart is required but does not force us to do it immediately.

Configuring a Project to Use C# 9

In .NET projects, we set the target framework in the project file. There is no option to set .NET 5 as a target framework within an existing project. Therefore, we open the project file in a text editor and manually change the project file.

Update: The latest Visual Studio update actually allows us to change the target framework to .NET 5 in the project settings.

First, we change the value of the TargetFramework property to net5.0, and second, we add the LangVersion property and set it to preview.

With those two small changes applied, we save the file and open the project in Visual Studio.

Use C# 9 features

Finally, we’re able to test the new features that come with C# 9 in our project. 

We are not going deeper into all the new features that come with C# 9 within this video. There is a high chance that I will make a dedicated video about it in the future. Don’t forget to subscribe, so you don’t miss out.

To test if C# 9 is available, we use the new Init-Only Properties feature. We create a new class and name it Person. Next, we create a string property called Name. Instead of writing a default set and get, we change the set part of the definition to init.

To show what this feature does, we go back to the Program file. Within the Program class, we create an instance of the Person class and use the object initializer syntax to assign a value to the Name property. Without the C# 9 Init-Only Property feature, this code would not compile.

Of course, you can use whatever C# 9 feature you want to test. Please let me know what C# 9 features you like the most in the comments below. 

Enjoy working with C# 9.

 

Claudio Bernasconi

I'm an enthusiastic Software Engineer with a passion for teaching .NET development on YouTube, writing articles about my journey on my blog, and making people smile.