26

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?

Community
  • 1
  • 1
serhio
  • 28,010
  • 62
  • 221
  • 374
  • 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. – Conrad Frix Apr 02 '14 at 14:44
  • Maybe not answering the question, but for reference: [What are the correct version numbers for C#?](http://stackoverflow.com/questions/247621/) – default Apr 02 '14 at 14:45
  • 1
    @Thorsten Dittmar : No. The same framework can use different C# versions (compilers) – serhio Apr 02 '14 at 14:45
  • 1
    @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 Apr 02 '14 at 14:47
  • 1
    @ThorstenDittmar see the link in the question. – serhio Apr 02 '14 at 14:49
  • I don't know how to do it, but if you read through the [documentation of MSBuild](http://msdn.microsoft.com/en-us/library/dd393574.aspx) (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. – Scott Chamberlain Apr 02 '14 at 15:13
  • 1
    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. – Hans Passant Apr 02 '14 at 15:56
  • this question needs attention i guess, not received any good answer yet – Ehsan Sajjad Mar 09 '16 at 15:25

5 Answers5

25

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://learn.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.

Lex Li
  • 60,503
  • 9
  • 116
  • 147
  • 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 – MaorB May 20 '20 at 13:46
  • @MaorB only C++ projects have been designed from ground up to support that scenario. C# projects are not. – Lex Li May 20 '20 at 14:00
  • 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? – MaorB May 21 '20 at 15:15
  • 1
    @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. – Lex Li May 21 '20 at 15:28
3

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.

Aik
  • 3,528
  • 3
  • 19
  • 20
  • 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](http://stackoverflow.com/q/22812388/185593) which should show different behavior under the older compiler. – Scott Chamberlain Apr 02 '14 at 15:01
  • 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? – serhio Apr 02 '14 at 15:04
  • 1
    @Scott Chamberlain It seems VS2010 upgrade this attribute automatically to 4.0: http://stackoverflow.com/questions/2770361/use-msbuild-3-5-with-visual-studio-2010 – Aik Apr 02 '14 at 15:29
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

SENya
  • 1,083
  • 11
  • 26
0

One way to tell the language version the compiler support is csc -langversion:?

and get response like

Supported language versions:
default
1
2
3
4
5
6
7.0
7.1
7.2
7.3
8.0
9.0 (default)
latestmajor
preview
latest
KudoofTheQ
  • 21
  • 5
-1

To indicate to Visual Studio which language version to use there's a compiler option called /langversion
You can find more about it here.
It can be set programmatically too, as stated here.
The compiler can compile in different versions of the language, the language version is not directly related to that of the framework, but often to use a language feature there's a minimum framework for which it can work.
Just few minutes ago I have compiled in VS 2015 a dll which uses the string interpolation of c# 6.0

var version = 4;
var output = $"{version}";

and has the framework 4.0 as target. It compiles and works fine. This post explains versions entanglement, but only for older c# versions.

  • 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. – Servy Jul 11 '17 at 17:12