As .NET 5 is rolled out, so too is another version of C#, this time being C# 9. I’m actually pretty excited for some of the features that, fingers crossed, should make it into the final release. But as always, I don’t want to wait till the official release, and instead jump in early and play with all the new shiny things.
So in actual fact, getting setup with C# 9 is basically the same as getting setup with .NET Preview 5. And luckily for you, we have a shiny guide right here : https://dotnetcoretutorials.com/2020/05/22/getting-started-with-net-5-and-the-msbuild-net-5-cliffnotes/. But for the cliffnotes of that post :
- Download and install the latest .NET 5 SDK from here : https://dotnet.microsoft.com/download/dotnet/5.0
- Ensure that your version of Visual Studio 2019 is atleast 16.7 by clicking Help => Check For Updates inside Visual Studio. If in doubt, update.
- Go Tools => Options inside Visual Studio, then select “Preview Features” and tick the box that says “Use previews of the .NET Core SDK“. Then restart Visual Studio.
Once the .NET 5 Preview SDK is installed and setup, then the only thing you need to do is edit your .csproj file and add a lang element like so :
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net5.0</TargetFramework> <LangVersion>9.0</LangVersion> </PropertyGroup> </Project>
If you’ve done all of that and you get either of these errors :
The reference assemblies for .NETFramework,Version=v5.0 were not found.
Invalid option '9.0' for /langversion. Use '/langversion:?' to list supported values.
Then here’s your quick and easy troubleshooting list :
- Are you sure you’ve got the latest .NET 5 SDK installed? Remember it’s the SDK, not the runtime, and it’s .NET 5.
- Are you sure you’re Visual Studio is up to date? Even if you have the SDK installed, Visual Studio plays by it’s own rules and needs to be updated.
- Are you using Visual Studio 2019? Any version lower will not work.
- Are you sure you enabled Preview SDKs? Remember Tools => Options, then “Preview Feature”
Keeping Up To Date
A new preview version of .NET 5/C#9 comes out every few months. So if you’re reading about a new C# 9 or .NET 5 feature that someone is using but you can’t seem to get it to work, then always head back to https://dotnet.microsoft.com/download/dotnet/5.0 and download the latest preview version and install. Similar for Visual Studio, while typically less of an issue, try and keep it up to date as so often latest features not working are simply that I’m on a version that was perfectly fine a month ago, but is now out of date.
C# 9 Features
We are slowly working our way through all new C# 9 features, if you are interested in other new additions to the language, check out some of the posts below.
All of these will be covered in detail coming soon!