Search code examples
c#asp.netsyntaxcompilationvisual-studio-2022

What C# compiler am I running and what is the latest version of C# syntax it supports?


I am overhauling a legacy solution that contains several projects, in their csproj files some have <LangVersion>default</LangVersion> and others have a version specified such as <LangVersion>7.3</LangVersion>

Now, I have recently discovered that <LangVersion>default</LangVersion> basically instructs that the latest major version of C# syntax that is available to the compiler can be utilised.

The usual search around is not unveiling this information.

P.S. I am utilising Visual Studio 2022 Pro and .Net 4.7.2 with .Net 4.81 installed and available on the dev machine, developing a web application, i.e. asp.net, with a variety of class libraries.

Edit 1: Worth noting that when setting the language version to 11 in the .props file in the solution folder and using #error version, in the tab that lists errors it says Language version 7.3 but in the 'Output' tab it correctly reports Language version 11.


Solution

  • Type into your C# file:

    #error version
    

    and it will tell you; for example, I see

    Error CS8304 Compiler version: '4.8.0-3.23504.4 (81d92746)'.

    Language version: preview.

    (the top number is the actual compiler version; the bottom number is after applying <LangVersion> etc)

    In general: avoid using <LangVersion>default</LangVersion> etc because it is ambiguous and depends on the build tools being used; a specific <LangVersion>10</LangVersion> is preferable (often in a Directory.Build.props file so it doesn't need to be duplicated), as it will give suitable clear messages when not available. This can be combined with global.json to ensure suitable build tool versions.