Last week I was working on a C# project in VS2019 and I wanted to use some language features from C# 8.0 (in particular ‘using declarations’).
So how do we check if a project is using C# 8.0? The logical place to look is the project settings. Right-click on the project and select Properties=>Build. Scroll down and click the ‘Advanced…’ button and you should see the following:
In previous versions of Visual Studio, the ‘Language version’ drop down showed the available versions.
If you click on ‘Why can’t I select a different C# version?’ you are taken to a Microsoft web page entitled ‘C# language versioning’. In it, they explain that it is not possible to change the default C# version via the UI, but it is possible to manually edit the .csproj
file to add LangVersion
tag. For example, to specify C# 8.0, you could add the following PropertyGroup
section:
<PropertyGroup>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
Alternatively, you can use the -langversion
compiler option. For example:
csc.exe -langversion:8.0 ...
Default compiler version
If you don’t specify a compiler version, the default is based on the framework you are using. The ‘C# language versioning’ page mentioned above provides a useful table. The C# version defaults to 8.0 if you are using .NET Core 3.x or .Net Standard 2.1. However, if you are using the latest .NET Framework, the default compiler version is C# 7.3.
Note that we can see which framework our project uses by opening the project properties and checking the Target framework
drop down in the Application tab.
Listing the available C# versions
Since there is no longer a drop down list in the project properties, how do we see which versions are installed on a system?
One way is to launch the Visual Studio 2019 Native Tools Command Prompt and enter the following:
csc -langversion:?
Conclusion
It would be nice if there were a fail-safe way of knowing which compiler was being used for a specific project. Unfortunately, there is nothing in the UI or compiler output to show this. But as we have seen, it is possible to explicitly specify the version by editing the project XML. And if we visit Microsoft’s ‘C# language versioning’ page, we can use the Defaults table to work out what the default C# version is for a specific target framework.