我想知道我使用的是哪个版本的c#。 如果我要使用python,我会从命令行执行类似python -V的操作,或者键入:

import sys
print sys.version

在PHP中,我会这样做:phpinfo();java中:java -version

但是我不知道如何在c#中实现这一点。

这个问题并没有回答这个问题,尽管它的名字表明它应该回答这个问题。

我知道这取决于。net框架,但是否有一种编程方式来确定我的框架?我的意思是不需要进入目录并检查。net文件夹的名称。


当前回答

前面的大部分答案都是正确的。只是巩固3,我发现这很简单,超级快。此外,#1现在在VS2019中是不可能的。

Visual Studio:项目>属性>构建>高级 这个选项现在被禁用了,默认语言由VS自己决定。详细的原因可以在这里找到。 在代码中任意位置输入“#error version”(.cs),鼠标悬停。 进入“Visual Studio开发人员命令提示符”并运行以下命令。 csc比如langversion: ?

阅读更多提示:这里。

其他回答

这里概述了. net框架和编译器版本是如何关联、设置和修改的。每个项目都有一个。net框架的目标版本,例如版本3。X或2。x。. net框架包含运行时类型和组件。

Visual Studio版本安装和. net框架版本决定了可以使用的兼容c#语言版本和编译器选项。Visual Studio项目中使用的默认c#版本和选项是与所使用的. net框架版本兼容的最新语言版本。

在Visual Studio 2011中查看或更新项目中的框架或c#语言:

right click the project within Solution Explorer and select Properties select 'Application' in the left navigation pane. Under Target framework: is the .NET framework version. Select the down arrow to see all available framework versions. select 'Build' in the left navigation pane. In the 'General' section of the pane next to 'Language Version:' is the c# compiler language version being used, for example 'default' or c# 5.0 select the down arrow in the 'Language Version:" dropdown to see all available language versions. If 'default' is the c# version being used, the latest compatible c# language version will be used.

要查看'default'的确切编译器语言版本,请在已安装的Visual Studio版本的开发人员命令提示符中输入以下内容。例如,在Windows开始图标中选择图标:“VS2011的开发人员命令提示符”,然后输入:

csc -langversion默认:

微软(R) Visual c#编译器版本4.7.3062.0的c# 5

对于Windows,您在命令/搜索程序行中运行dev,并为vs选择开发人员命令提示符,然后您将运行

csc

现在你得到了类似于

Microsoft (R) Visual C# Compiler version 2.6.0.62329 (5429b35d)
Copyright (C) Microsoft Corporation. All rights reserved.   

对于Windows,如果以cmd终端启动

cd C:\Windows\Microsoft.NET\Framework\
dir

现在你看到所有的目录和文件在。net \Framework\ Please,选择v…比如,最近去那里,

cd v4.0.30319

Run

csc

你会看到关于c#编译器版本的信息,这可以是类似的

Microsoft (R) Visual C# Compiler version 4.7.2556.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

这取决于你使用的。net框架。请看Jon Skeet关于版本的回答。

以下是他的回答的简短版本。

c# 1.0与。net 1.0一起发布 c# 1.2(很奇怪);发布 .NET 1.1 c# 2.0随。net 2.0发布 c# 3.0与。net 3.5一起发布 c# 4.0随。net 4发布 c# 5.0与。net 4.5一起发布 c# 6.0与。net 4.6一起发布 c# 7.0与。net 4.6.2一起发布 c# 7.3与。net 4.7.2一起发布 c# 8.0随NET Core 3.0发布 c# 9.0与NET 5.0一起发布 c# 10.0与NET 6.0一起发布 c# 11.0随NET 7.0发布

如果你正在使用VS2015,那么按照下面的步骤来找到相同的:

右键单击项目。 单击Properties选项卡。 从属性窗口选择“构建”选项。 然后点击Advance按钮。 在那里你会找到语言版本。

下面的图片显示了相同的步骤:

步骤1:

步骤2:

你所使用的c#版本完全取决于你所使用的。net版本。

如果你使用visual studio进行开发,你可以选择。net框架版本 与之相关的c#版本也随之而来

以下是已知的c#版本:

C# 1.0 released with .NET 1.0 and VS2002 (January 2002) C# 1.2 (bizarrely enough); released with .NET 1.1 and VS2003 (April 2003). First version to call Dispose on IEnumerators which implemented IDisposable. A few other small features. C# 2.0 released with .NET 2.0 and VS2005 (November 2005). Major new features: generics, anonymous methods, nullable types, iterator blocks C# 3.0 released with .NET 3.5 and VS2008 (November 2007). Major new features: lambda expressions, extension methods, expression trees, anonymous types, implicit typing (var), query expressions C# 4.0 released with .NET 4 and VS2010 (April 2010). Major new features: late binding (dynamic), delegate and interface generic variance, more COM support, named arguments and optional parameters C# 5.0 released with .NET 4.5 in August 2012.

参考Jon Skeet的c#版本回答