Hi guys, today I’m writing about a little bit tip to select a specific C# version.
You just imagine that you want to use the Pattern matching (a feature of C# 8) but you have just created a project. That is a problem because C# 8 is a preview version at the moment (major version) and only projects target netcoreapp3.0 or netstandard2.1 (both in preview) will use the C# 8.0 language (also in preview).
In other words, if you had already created a project, for example, in the traditional framework o net core 2.2 by default your project would use C# 7.3 (the latest released version).
The big questions are:
- What is my current c# version?
- Can I change my current C# version to C# 8 to use Pattern Matching?
What is my current c# version?
Ok, how I write above our c# version depend about several factors: dot net framework installed in the server / local computer, project target (traditional framework / net core 2.1 / net core 2.2 / net core 3 preview / etc).
If our project is targeted with a preview framework version that has a corresponding preview language version, the language version used is the preview language version. When your project doesn’t target with a preview framework, the language version used is the latest minor version.
Ok enough theory, first:
right click in the project –> properties –> Build –> Advance:
Ok, our project is using the C# lastest supported minor version (lastest).
The second step, we need to find the Developer Command Prompt for VS 2019 (in my case I’m using this IDE version). Then we type:
You can see 7.3 C# Version is the default ( latest supported minor version ) but I have the 8.0 beta version installed too. In other words, our project is using C# 7.3.
Ok, you imagine that we need to use pattern matching in our project, bad luck we can’t use this feature with the current C# Version 7.3:
The sintax is correct but our current language version doesn’t support pattern matching, we need to change it to C# 8 to use this feature.
At the moment our project doesn’t compile 🙁
How do I change my C# current version?
Ok, we’ll change our c# version. First, we make right click in the project –> properties –> Build –> Advance –> Change the C# Version to C# 8
Finally, we should compile the solution:
Ok, perfect our solution is working with C# 8 and supports pattern matching! It’s great 😉 We could change the language version!
We can see the project file:
You can see how our project works with c# 8 though its target is netcore 2.1 that by default works with C# 7.3 (current stable version).
For more details, you can check the source: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version