VS 2017开发人员命令提示中的语言版本
我创建了以下程序
public class Program
{
static void Main()
{
System.Console.WriteLine("Hello, World!");
}
}
在命令提示符下编译代码时( c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc hello.cs ),返回
When compiling the code in the command prompt (c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc hello.cs) which returns
Microsoft (R) Visual C# Compiler version 4.7.3056.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.
This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240
程序仍然可以编译,所以我认为没什么大不了的。根据其他搜索,我似乎认为这则消息仅供参考。
The program still compiles so I thought no big deal. Based on other searches I've done I it seemed as if this message is only informational.
但是,对于该程序
public class Program
{
static void Main()
{
System.Console.WriteLine(DateTime.Now.DayOfWeek);
}
}
返回值为
Microsoft (R) Visual C# Compiler version 4.7.3056.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.
This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240
hello.cs(5,28): error CS0103: The name 'DateTime' does not exist in the current context
根据复数视频我应该可以正常工作跟着。经过一些搜索后,许多答案都涉及更改项目中使用的C#版本,但似乎无法将其转换为在命令提示符下工作。需要更改什么?
which should work fine according to the pluralsight videos I'm following along with. After some searches many of the answers refer to changing the version of C# being used in the project but can't seem to translate that to working in the command prompt. What needs to change?
系统上有几种版本的C#编译器(csc.exe)。因此,
There are several versions of C# compiler (csc.exe) available on the system. So you have
- Microsoft编译器作为框架的一部分,例如
c:\Windows\Microsoft.NET \Framework\v4.0.30319\csc.exe
- 作为Visual Studio一部分的Microsoft编译器(Roslyn),例如
C :\Program Files(x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn\csc.exe
- Microsoft Compilers as Part of The Framework, like
c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
- Microsoft Compilers (Roslyn) as Part of Visual Studio, like
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn\csc.exe
使用
c:\Windows\Microsoft .NET\Framework\v4.0.30319\csc
c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc
您正在使用.NET Framework附带的C#编译器,并且此编译器仅支持 C#5.0 的语言功能。
You are using the C# compiler that shipped with .NET Framework, and this compiler only supports language features up to C# 5.0.
例如,它无法编译以下程序,该程序使用新的C#6.0语言功能在catch / finally块中等待
。
For example, it cannot compile the following program which uses the new C# 6.0 language feature Await in catch/finally blocks
.
class Program
{
static void Main(string[] args)
{
}
async Task<int> GetVAsync()
{
try
{
}
catch
{
//gives error CS1985: Cannot await in the body of a catch clause
await Task.Delay(1000);
}
return 3;
}
}
警告消息的含义 >,
您 选择 使用的编译器(.NET Framework附带的编译器)无法使用编译C#5.0之后引入的新功能。如果不使用任何新功能(如您的示例),则编译可能会成功。但是,最好使用Visual Studio附带的 new 编译器-Roslyn-来充分利用编译器的全部功能。
the compiler you choose to use (the one shipped with .NET Framework) is not capable of compiling new features introduced after C# 5.0. The compilation may succeed if don't use any new feature, just like your example. But you have better use the newer compilers - Roslyn that shipped with Visual Studio - to take advantage of the full power of the compiler.
解决方案
在命令行中使用它(路径可能因VS版本而异)
Use this in your command line (the path may differs based on your VS edition)
C:\Program Files(x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn\csc.exe你好.cs
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn\csc.exe hello.cs
参考
*用户Lex Li在这篇文章。
* user Lex Li gave an excellent explanation on the versioning history of the C# compilers in this post.