Difference between C# compiler version and language version

11,342

Solution 1

As nobody gives a good enough answer, I will have a try now.

First, C# has its version history published by Microsoft now (coming from MVP posts obviously),

https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history

So you can easily see what new features are added for each new releases.

Second, we will talk about the compiler releases, which initially were part of .NET Framework.

Below I list a few milestones (might not be 100% correct, and some versions might be skipped),

  • csc.exe 1.0 (?) for .NET Framework 1.0 (implements C# 1.0).
  • csc.exe 2.0 (Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.8745) for .NET Framework 2.0 (implements C# 2.0, as well as 1.0 for compatibility).
  • csc.exe 3.5 (Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.8763) for .NET Framework 3.5 (implements C# 3.0, and older versions).
  • csc.exe 4.0 (?) for .NET Framework 4.0 (implements C# 4.0, and older).
  • csc.exe 4.x (like Microsoft (R) Visual C# Compiler version 4.7.2053.0) for .NET Framework 4.5 and above (implements C# 5.0, and older). Note that the version numbers vary a lot (from 4.x to 12.x) based on the .NET Framework on your machine (4.5.0 to 4.7.1).

Then Microsoft made the old csc.exe obsolete (as they were native executable), and shipped Roslyn based compiler instead (though still csc.exe). In the meantime, C# compiler is no longer part of .NET Framework, but part of VS.

It was the same time, that C# compiler, language version, and .NET Framework are fully decoupled, so that you can easily use multi-targeting.

  • Roslyn csc.exe 1.x (?) implements C# 6.0 and older. Shipped with VS2015.
  • Roslyn csc.exe 2.x (like Microsoft (R) Visual C# Compiler version 2.4.0.62122 (ab56a4a6)) implements C# 7.x and older. Shipped with VS2017.

Ok, enough background. Back to your questions.

Q1: How to find out what C# version (not the compiler one, but the language one) uses VS to build my concrete project?

Answer: You can easily see from project settings that what language version is used.

Advanced settings

If you don't choose an explicit version, it can automatically use the latest version supported by the csc.exe compiling the project.

Note that @Servy commented under @DaniloCataldo's answer about the langversion switch with more details. That switch has its design goals and limitation. So for example even if you force Roslyn 2.x compiler to compile your project based on C# 4.0, the compiled result would be different from what C# 4.0 compiler does.

Q2: Is there a strict, clear and transparent link between the C# compiler and language versions?

Answer: Please refer to the background I described above, I think that already answered this part. There is a strict, clear and transparent link.

Q3: Can I indicate to Visual Studio (in case of migration issues from one Studio version to another) to use different compiler version for my concrete solution?

Answer: A Visual Studio release (like VS2019) sticks to an MSBuild release (16.x), so a dedicate version of C# compiler. So in general Q3 is duplicate to Q1, as you can only change language version.

There are a bunch of NuGet packages to override C# compiler used by a project, such as https://www.nuget.org/packages/Microsoft.Net.Compilers.Toolset. However, Microsoft states that "Using it as a long term solution for providing newer compilers on older MSBuild installations is explicitly not supported", so you really shouldn't explore that route.

Solution 2

In the past Visual Studio 2005 was fixed only to one .Net version and C# compiler delivered with this version. In case you want use newer version of VS you have to switch Visual Studio as well. Now Visual studio can target to more than one .Net version and it can even mix new C# compiler with old .Net framework (lambdas or extension methods in .Net 2.0). Simply C# compiler version is related to C# language version.

You can check your compiler version in project file (open it as xml) and there is ToolsVersion attribute of Project element.

In my specific project there is ToolsVersion="4.0" and my target project is .Net 2.0. It means I can use new language construct in old framework which is not possible in VS2005.

Solution 3

Just to add to the previous answer. You should be aware that while the C# language and the C# compiler are separate from .Net framework, they still depend on it.

For example, the C# 5 has await/async language feature, but you can't use it with the .Net 4, at least not without some extra actions and nuget packages.

On the other hand, you still can use the nameof or null propagation features of C# 6 with the .Net 4.0 because they are implemented purely by compiler

Share:
11,342

Related videos on Youtube

serhio
Author by

serhio

I like .NET and started learning PHP. email: gserhio[at]gmail[.]com

Updated on June 04, 2022

Comments

  • serhio
    serhio almost 2 years

    There was time I thought that the Framework version and the C# version are the same things, so once you install the next Framework version on the computer, you should use it.

    Then I found out that the framework is not linked directly with the C# version, and on the same machine multiple C# compilers can be cohabitate, so probably the compiler and C# version should be the same.

    Now I understand that the compiler version and the C# version are not the same...


    Visual Studio Command Prompt (2010): C:\>csc

    Microsoft (R) Visual C# Compiler version 4.0.30319.33440
    for Microsoft (R) .NET Framework 4.5


    Developer Command Prompt for VS 2013: C:\>csc

    Microsoft (R) Visual C# Compiler version 12.0.30110.0
    for C# 5


    enter image description here

    we can see that
    - VS 2010 uses a compiler version 4.0 for the C#4 (?? I just can suppose it, because not explicitly mentioned);
    - VS 2013 uses the compiler version 12.0 fo the C# 5 (this is explicitly mentioned)

    Knowing that compiling using different language versions could bring different results to the user

    Questions

    • How to find out what C# version (not the compiler one, but the language one) uses VS to build my concrete project?

    • Is there a strict, clear and transparent link between the C# compiler and language versions?

    • Can I indicate to Visual Studio (in case of migration issues from one Studio version to another) to use different compiler version for my concrete solution?

    • Conrad Frix
      Conrad Frix about 10 years
      Given that a compiler is software that takes a bunch of files as input and outputs another file, and c# is basically a specification it doesn't surprise me that they are not versioned the same way.
    • default
      default about 10 years
      Maybe not answering the question, but for reference: What are the correct version numbers for C#?
    • serhio
      serhio about 10 years
      @Thorsten Dittmar : No. The same framework can use different C# versions (compilers)
    • serhio
      serhio about 10 years
      @Default: The related question links .NET Framework versions with C# versions. I ask about the same link between compilers and language, don't care about framework.
    • serhio
      serhio about 10 years
      @ThorstenDittmar see the link in the question.
    • Scott Chamberlain
      Scott Chamberlain about 10 years
      I don't know how to do it, but if you read through the documentation of MSBuild (the program Visual Studio 2012 and newer calls behind the scenes that compiles your project when you hit compile) you may be able to figure out how to make it use other compilers than the csc bundled with the version of visual studio you are using.
    • user1703401
      user1703401 about 10 years
      It is part of a significant msbuild reorganization in VS2013. Type "where csc.exe" to see what's happening. The point of using commands like the Visual Studio Command Prompt is to let Microsoft worry about getting this right.
    • Ehsan Sajjad
      Ehsan Sajjad about 8 years
      this question needs attention i guess, not received any good answer yet
  • Scott Chamberlain
    Scott Chamberlain about 10 years
    I don't think changing the tools version changes the compiler version, I just tested it and setting tools version to a lower number did not change the output of a test program which should show different behavior under the older compiler.
  • serhio
    serhio about 10 years
    Thanks for answering. Is there any reference where indicated how concretely compiler and language versions are related? Say, I want to use C#5, what compiler version should I target?
  • Aik
    Aik about 10 years
    @Scott Chamberlain It seems VS2010 upgrade this attribute automatically to 4.0: stackoverflow.com/questions/2770361/…
  • Servy
    Servy almost 7 years
    The langversion switch doesn't change the actual language version (contrary to what the name implies). It simply causes usages of certain features to result in compile time errors. There are many changes between C# versions besides just adding new features, and as a result code compiled on a C# 7 compiler with the langversion switch set to 3.0 will behave differently than code compiled on a C# 3.0 compiler.
  • MaorB
    MaorB about 4 years
    It's been a while, but thank you for the detailed answer. You wrote in answer to question 3, duplicate to Q1, but i don't think it answers the question. I still don't understand how to control the compiler version? For instance, i want a newer VS version to compile with older compiler version. Is this possible and if so, how? Thanks
  • Lex Li
    Lex Li about 4 years
    @MaorB only C++ projects have been designed from ground up to support that scenario. C# projects are not.
  • MaorB
    MaorB about 4 years
    Ok Thanks. So in case i do want to compile with the same compiler version tjat I used when I first created the app, i need to use the same VS version I originally used, right?
  • Lex Li
    Lex Li about 4 years
    @MaorB right. You'd better use the old VS release so as to keep your environment stable enough. Newer VS releases is bound to newer MSBuild releases and C# compiler releases, so no way back from there.