In this article, we will learn how to check the C# version in Visual Studio 2022. However, this solution will work with all versions of Visual Studio.
Recently, I came across a real-time project in which I had to migrate the .Net Core older version application to .NET Core 3.1.
In another blog, the Migration step is explained. Read this blog: migration process step by step.
But before that, I would like to explain a piece of very basic information that every .NET developer should be aware of and that is to verify the C# version in Visual Studio.
How to know which C# version is being used in Visual Studio?
Every .NET version works with a specific version of C#. In this section of the article, we will see how to check the C# version in Visual Studio 2022.
I copied one project which was built in Visual Studio 2017 and .NET Core 2.2, then I opened the same project in Visual Studio 2022.
And I see so many errors mentioning similar lines. The error statement was –
Feature 'top-level statements' is not available in C# 8.0. Please use language version 9.0 or greater.
So, first I checked the C# version of the current project.
To check this, right-click on the Project name in Solution Explorer and select Build->Advanced
You will see the Language version 8.0 as shown in the article. The C# version may be different in your case.
How to change the C# Language Version in Visual Studio?
We are now able to check the C# version in Visual Studio. Now, we will change the c# language version in Visual Studio 2022.
So, to change the version of C# open the .csproj
file or right-click on the project name in Visual Studio and select the edit project option. Then, add the below line.
<LangVersion>10.0</LangVersion>
Complete .csproj
file snippet.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>10.0</LangVersion>
</PropertyGroup>
</Project>
I have used C# 10 because I am using Visual Studio 2022, you can change it according to the .NET Core version you are using and of course, it depends on the project requirements.
In this way, you can change the C# version of your project.
This solution could be similar to other Visual Studio versions as well. So, in this way, we can check the C# version in Visual Studio 2019.
You may check this link for C# Version History – Evolution of .NET