C#编译器版本和语言版本的区别 [英] Difference between C# compiler version and language version

查看:33
本文介绍了C#编译器版本和语言版本的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有段时间我觉得Framework版本和C#版本是同一个东西,所以一旦你在电脑上安装了下一个Framework版本,你就应该使用它.

然后我发现框架并没有直接和C#版本挂钩,而且在同一台机器上可以有多个C#编译器共存,所以编译器和C#版本应该是一样的.

现在明白编译器版本和C#版本不一样了...

<小时>

Visual Studio 命令提示符 (2010):C:>csc

Microsoft (R) Visual C# 编译器版本 4.0.30319.33440
适用于 Microsoft (R) .NET Framework 4.5

<小时>

VS 2013 的开发人员命令提示符:C:>csc

Microsoft (R) Visual C# 编译器版本 12.0.30110.0
对于 C# 5

<小时>

我们可以看到
- VS 2010 为 C#4 使用了 4.0 版编译器(?? 我只能假设它,因为没有明确提到);
- VS 2013 使用 C# 5 的 12.0 版编译器(已明确提及)

知道

如果不选择显式版本,它会自动使用编译项目的csc.exe支持的最新版本.

请注意,@Servy 在@DaniloCataldo 关于 langversion 开关的回答下评论了更多详细信息.该开关有其设计目标和局限性.因此,例如,即使您强制 Roslyn 2.x 编译器基于 C# 4.0 编译您的项目,编译结果也会与 C# 4.0 编译器不同.

Q2:C#编译器和语言版本之间是否存在严格、清晰、透明的联系?

回答:请参考我上面描述的背景,我想这部分已经回答了.有严格、清晰、透明的链接.

问题 3:我可以向 Visual Studio 指示(以防从一个 Studio 版本迁移到另一个版本)为我的具体解决方案使用不同的编译器版本吗?

答案:Visual Studio 版本(如 VS2019)坚持 MSBuild 版本 (16.x),因此是 C# 编译器的专用版本.所以一般来说 Q3 与 Q1 重复,因为您只能更改语言版本.

<块引用>

有一堆 NuGet 包可以覆盖项目使用的 C# 编译器,例如 https://www.nuget.org/packages/Microsoft.Net.Compilers.Toolset.但是,Microsoft 表示明确不支持将其用作在较旧的 MSBuild 安装上提供较新编译器的长期解决方案",因此您真的不应该探索该路线.

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


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?

解决方案

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.

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.

这篇关于C#编译器版本和语言版本的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆