We can probably all agree that C# 8.0 is a version of the language packed with many new features. The problem is that some developers think that you can only use C# 8 if you are using .NET Core 3 or .NET Standard 2.1. That’s not entirely true. Enabling C# 8 in any .NET project is easier than you think. In this article, I am going to show you how to use C# 8 in Visual Studio 2019.
LOOKING FOR SOMETHING ELSE? TRY THESE LINKS INSTEAD:
- Visual Studio IntelliCode – AI for your Code
- Extension Methods are Easy with C#
- The Unbelievably Powerful Multi Select. Increase Productivity Now
- ApexSQL Provides Excellent SQL Tools for Free
- Script Table Data In SQL Server Management Studio
If you are short on time, check out the video tutorial on how to enable C# 8 in Visual Studio 2019.
If you like the video, leave a comment.
Install and start using C# 8 in Any .NET Project
To start using C# 8 in your .NET Projects, you will need to modify your csproj file. Look for the LangVersion tag and change it as follows:
Here is what it should look like.
This is all there is to it. There are some proviso’s though.
Some C# 8 Types Not Included
Some types such as IAsyncEnumerable are not included. There is a workaround though. Remember how developers needed to install a NuGet package to bring in ValueTulple? Well, you can do the same here.
Indexes and Ranges Limitation
Using C# 8 in non-.NET Core 3 or .NET Standard 2.1 projects does have some limitations. But these are few and far between. For example, indexes and ranges are runtime features and this means that they will not be available to you. If you try to use these, your code will simply not compile.
Should you Enable C# 8?
Enabling C# 8 in any .NET Project is super easy. If you can live with the limitations and exclusions of some types, there is no reason why you shouldn’t try using C# 8. If you are not familiar with all the features of C# 8, then have a look at the following list.
C# 8 Features
Here are some of the new features and enhancements to C# 8:
- Readonly members
- Default interface methods
- Pattern matching enhancements:
- Switch expressions
- Property patterns
- Tuple patterns
- Positional patterns
- Using declarations
- Static local functions
- Disposable ref structs
- Nullable reference types
- Asynchronous streams
- Indices and ranges
- Null-coalescing assignment
- Unmanaged constructed types
- Stackalloc in nested expressions
- Enhancement of interpolated verbatim strings
During the coming weeks, we will explore some of these new C# 8 features, so stay tuned.
Which Language Version am I using for my version of .NET?
So some time has passed since I originally wrote this article and .NET 5 has been released. You might be wondering which version of C# am I using? Well is all depends on the version of .NET that you are targeting. Here is a handy table to help you see which version of C# your project will target.
|Target framework||version||Default C# language version|
|.NET Core||3.x||C# 8.0|
|.NET Core||2.x||C# 7.3|
|.NET Standard||2.1||C# 8.0|
|.NET Standard||2.0||C# 7.3|
|.NET Standard||1.x||C# 7.3|
|.NET Framework||all||C# 7.3|
While I know that some of you will just create new projects with the default (current .NET 5.0 version) and use C# 9. If you have an old project that you don’t want to upgrade to a later version of .NET, the above article might come in handy.