Search code examples
c#version

Which version of C# am I using


I want to find out which version of C# I'm using. If I would be using python I would do something like python -V from the command line, or type:

import sys
print sys.version

In PHP I would do something like this: phpinfo(); in java: java -version

But I was not able to find how to achieve this in C#.

This question does not answer it, although the name suggests that it should.

I got that it depends on the .NET framework, but is there a programmatic way of figuring out my framework? I mean without going to the directory and checking the name of my .NET folders.


Solution

  • You can't find out what language C# version was used by compiler to produce the assembly and hence there is no code you can write to detect it at run-time.

    At compile time C# version is defined by setting in the project file for project (can be viewd in VS settings of the project as shown in another answer) and explicit or implicit switch passed to compiler if compiling directly with CSC (How do I tell the Roslyn C# compiler to use a specific version of .NET?)

    The only version you can get at run-time is the version of the framework - look at version of one of main Assemblies i.e.

     Console.Write(typeof(string).Assembly.ImageRuntimeVersion);
    

    Getting version of C# compiler is somewhat harder, but you should be able to guess version by checking what framework version is used.

    If you are using command line compiler (csc.exe) you can check help to see version of the compiler itself (also you'd need to know Framework version anyway:

    C:\Windows\Microsoft.NET\Framework\v4.0.30319>csc /?
    Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
    

    Note that each version of the compiler can target all previous versions of C# - following Roslyn compiler command reporting supported C# versions 1-11:

    "C:\Program Files\Microsoft Visual 
      Studio\2022\Preview\MSBuild\Current\Bin\Roslyn\csc.exe" /langversion:?