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. Here’s how.
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.
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.