C# 与 C# 编译器

C# vs C# Compiler

C# 6 是否意味着 "Roslyn"?

C# 和 C# 编译器是一回事吗? 有很多关于 C# 和新的 C# 特性的文章,但 C# 是什么? 它是 C# 编译器,还是我需要了解其他相关信息?

以及如何查看我的 visual studio 使用哪个版本来编译代码? 我知道例如 vs 2015 默认使用 C# 6 但它是如何管理的,任何配置文件或其他东西?

我可以看到 visualstudio 在编译我的代码时使用的命令吗?例如在输出 Window 处? (类似于 "cl.exe ... main.c")

每个 C# 编译器的路径是什么?

Does C# 6 mean "Roslyn"?

有效,是的。没有官方规范(an unofficial draft exists, hopefully, that will improve in the near future). The Mono C# compiler 确实支持 C# 6.0,但据我所知,它正在被 Roslyn 编译器取代。

Is it the same thing C# and C# Compiler?

理论上,C# 语言(如其规范所定义,如果存在)与 Microsoft 开发的 C# 编译器 (a.k.a.Roslyn) 之间存在差异。在实践中,它们非常紧密地对应在一起。 C# 6.0 与 "the language supported by version 1.0 of the Roslyn compiler" 或 "the version of C# supported by VS 2015" 相同。 (同样适用于 C# 7.0、Roslyn 2.0 和 VS 2017。)

And how can see which version of my visual studio using to compile the code?

除非你做了一些不寻常的事情,比如自己编译 Roslyn 并在 VS 的实例中使用它,否则 VS 使用与其一起发布的 C# 编译器版本。这意味着 VS 2015 的 C# 6.0 和 VS 2017 的 C# 7.0。有关完整列表,请参阅 this table in the Wikipedia article about C#(查看第一列和最后一列)。

您还可以使较新的编译器使用该语言的早期版本进行编译。要在 VS 中执行此操作,请转到项目属性 → 构建 → 高级 → 语言版本。

I know that for example vs 2015 using C# 6 by default but how does it manages that, any configuration file or something else?

The C# extension for Visual Studio 包括一个内置的 C# 编译器,用于 IntelliSense 之类的东西。

为了实际构建项目,VS 使用 MSBuild,它是使用 .targets 文件配置的。具体来说,如果你想设置自己的路径为csc.exe,你可以使用CscToolPath 属性,例如在命令行上:msbuild /p:CscToolPath=my\csc\path 或将 <PropertyGroup><CscToolPath>my\csc\path</CscToolPath></PropertyGroup> 添加到您的 .csproj 文件。

Can I see the commands Visual Studio is using while compiling my code for example at Output Window?

一种方法是从 VS 的开发人员命令提示中 运行 msbuild(可能使用 /t:rebuild 以确保编译器实际上是 运行)。

例如,对于在 VS 2017 中创建的新 C# 控制台应用程序项目,我的输出是:

Microsoft (R) Build Engine version 15.1.548.43366
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 27. 3. 2017 16:39:39.
Project "C:\code\tmp\ConsoleApp2\ConsoleApp2.csproj" on node 1 (rebuild target(s)).
CoreClean:
  Deleting file "C:\code\tmp\ConsoleApp2\bin\Debug\ConsoleApp2.exe.config".
  Deleting file "C:\code\tmp\ConsoleApp2\bin\Debug\ConsoleApp2.exe".
  Deleting file "C:\code\tmp\ConsoleApp2\bin\Debug\ConsoleApp2.pdb".
  Deleting file "C:\code\tmp\ConsoleApp2\obj\Debug\ConsoleApp2.csprojResolveAssemblyReferen
  ce.cache".
  Deleting file "C:\code\tmp\ConsoleApp2\obj\Debug\ConsoleApp2.exe".
  Deleting file "C:\code\tmp\ConsoleApp2\obj\Debug\ConsoleApp2.pdb".
GenerateBindingRedirects:
  No suggested binding redirects from ResolveAssemblyReferences.
GenerateTargetFrameworkMonikerAttribute:
Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-t
o-date with respect to the input files.
CoreCompile:
  C:\Program Files (x86)\Microsoft Visual Studio17\Enterprise\MSBuild.0\bin\Roslyn\cs
  c.exe /noconfig /nowarn:1701,1702 /nostdlib+ /platform:anycpu32bitpreferred /errorreport:
  prompt /warn:4 /define:DEBUG;TRACE /highentropyva+ /reference:"C:\Program Files (x86)\Ref
  erence Assemblies\Microsoft\Framework\.NETFramework\v4.6.2\Microsoft.CSharp.dll" /referen
  ce:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.2\
  mscorlib.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework
  \.NETFramework\v4.6.2\System.Core.dll" /reference:"C:\Program Files (x86)\Reference Assem
  blies\Microsoft\Framework\.NETFramework\v4.6.2\System.Data.DataSetExtensions.dll" /refere
  nce:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.2
  \System.Data.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Frame
  work\.NETFramework\v4.6.2\System.dll" /reference:"C:\Program Files (x86)\Reference Assemb
  lies\Microsoft\Framework\.NETFramework\v4.6.2\System.Net.Http.dll" /reference:"C:\Program
   Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.2\System.Xml.dll
  " /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramewo
  rk\v4.6.2\System.Xml.Linq.dll" /debug+ /debug:full /filealign:512 /optimize- /out:obj\Deb
  ug\ConsoleApp2.exe /ruleset:"C:\Program Files (x86)\Microsoft Visual Studio17\Enterpri
  se\Team Tools\Static Analysis Tools\Rule Sets\MinimumRecommendedRules.ruleset" /subsyste
  mversion:6.00 /target:exe /utf8output Program.cs Properties\AssemblyInfo.cs "C:\Users\Svi
  ck\AppData\Local\Temp\.NETFramework,Version=v4.6.2.AssemblyAttributes.cs"
  Using shared compilation with compiler from directory: C:\Program Files (x86)\Microsoft V
  isual Studio17\Enterprise\MSBuild.0\bin\Roslyn
_CopyAppConfigFile:
  Copying file from "App.config" to "bin\Debug\ConsoleApp2.exe.config".
CopyFilesToOutputDirectory:
  Copying file from "obj\Debug\ConsoleApp2.exe" to "bin\Debug\ConsoleApp2.exe".
  ConsoleApp2 -> C:\code\tmp\ConsoleApp2\bin\Debug\ConsoleApp2.exe
  Copying file from "obj\Debug\ConsoleApp2.pdb" to "bin\Debug\ConsoleApp2.pdb".
Done Building Project "C:\code\tmp\ConsoleApp2\ConsoleApp2.csproj" (rebuild target(s)).


Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.33

注意 CoreCompile 部分,它显示了用于编译项目的命令。

获得相同输出的另一种方法是 change the MSBuild verbosity in VS 到正常、构建(或重建),然后查看输出 window。

What are the paths of every single C# compiler?

您可以通过打开该版本VS的开发者命令提示符和运行ning where csc来查看该版本VS对应的csc.exe位置是什么.对我来说,这为 VS 2015 Update 3 打印 C:\Program Files (x86)\MSBuild.0\Bin\csc.exe(版本 1.3.1.60616),为 VS 2017 打印 C:\Program Files (x86)\Microsoft Visual Studio17\Enterprise\MSBuild.0\Bin\Roslyn\csc.exe(版本 2.0.0.61501)。

路径不同的原因是因为 VS 2017 更改了其各种组件的安装位置。

在这两种情况下,还会打印第二行 C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe,这是旧的 Roslyn C# 5.0 之前的编译器(版本 4.6.1586.0)。