You are here
Home > Programming > Enabling C# 8 in Visual Studio 2019

Enabling C# 8 in Visual Studio 2019

using c# 8 in visual studio 2019

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:

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:

<LangVersion>8.0</LangVersion>

Here is what it should look like.

csproj file with langversion modified to target C# 8

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.

Just swing on over to NuGet and get Microsoft.Bcl.AsyncInterfaces and Microsoft.Bcl.HashCode.

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 frameworkversionDefault C# language version
.NET5.xC# 9.0
.NET Core3.xC# 8.0
.NET Core2.xC# 7.3
.NET Standard2.1C# 8.0
.NET Standard2.0C# 7.3
.NET Standard1.xC# 7.3
.NET FrameworkallC# 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.

Dirk Strauss
Dirk is a Software Developer from South Africa. He loves all things Technology and is slightly addicted to Jimi Hendrix. Apart from writing code, he also enjoys authoring books and articles. "I love sharing knowledge and connecting with people from around the world. It's the diversity that makes life so beautiful." Dirk feels very strongly that pizza is simply not complete without Tabasco, that you can never have too much garlic, and that cooking the perfect steak is an art he has almost mastered.
https://dirkstrauss.com

Similar Articles

Top