我想知道我使用的是哪个版本的c#。 如果我要使用python,我会从命令行执行类似python -V的操作,或者键入:
import sys
print sys.version
在PHP中,我会这样做:phpinfo();java中:java -version
但是我不知道如何在c#中实现这一点。
这个问题并没有回答这个问题,尽管它的名字表明它应该回答这个问题。
我知道这取决于。net框架,但是否有一种编程方式来确定我的框架?我的意思是不需要进入目录并检查。net文件夹的名称。
我想知道我使用的是哪个版本的c#。 如果我要使用python,我会从命令行执行类似python -V的操作,或者键入:
import sys
print sys.version
在PHP中,我会这样做:phpinfo();java中:java -version
但是我不知道如何在c#中实现这一点。
这个问题并没有回答这个问题,尽管它的名字表明它应该回答这个问题。
我知道这取决于。net框架,但是否有一种编程方式来确定我的框架?我的意思是不需要进入目录并检查。net文件夹的名称。
当前回答
从开发人员命令提示符(Visual Studio > View > Terminal)输入:
csc -langversion:?
将显示所有支持的c#版本,包括默认版本:
1
2
3
4
5
6
7.0 (default)
7.1
7.2
7.3 (latest)
其他回答
要获得框架的版本-查看一个主要程序集的版本。
Console.Write(typeof(string).Assembly.ImageRuntimeVersion);
获取c#编译器的版本有点困难,但你应该能够通过检查使用的框架版本来猜测版本。
如果你正在使用命令行编译器(csc.exe),你可以检查帮助查看版本(同时你也需要知道框架版本:
C:\Windows\Microsoft.NET\Framework\v4.0.30319>csc /?
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
默认情况下,以下是Visual Studio对应版本的c#编译器:
Visual Studio 2015: c# 6.0 Visual Studio 2013: c# 5.0 Visual Studio 2012: c# 5.0 Visual Studio 2010: c# 4.0 Visual Studio 2008: c# 3.0 Visual Studio 2005: c# 2.0 Visual Studio。Net 2003: c# 1.2 Visual Studio。Net 2002: c# 1.0
您也可以修改版本,请按照以下步骤。
打开项目属性窗口:
step 1. Right click on the Project Name
step 2. Select "Properties" (last option in menu)
step 3. Select "Build" from left hand side options and scroll till down
step 4. click on "Advance" button.
step 5. It will open a popup and there you will get "Language Version" dropdown
step 6. Select desired version of C# and Click "OK"
这里有一个奇怪的技巧,可以快速找到你正在使用的c#版本。编辑项目中的任何。cs文件,并在命名空间语句的末尾添加一个分号:
命名空间Whatever ->命名空间Whatever;
命名空间工作将以红色下划线显示;将鼠标悬停在它上面,它会显示如下内容: “CS8370:文件范围的命名空间在c#7.3中不可用。请使用10.0或更高版本的语言。”
在VS2022上使用。net Framework项目进行测试。如果使用的是。net或。net Core项目,那么这个特性可以很好地工作。
你所使用的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#版本回答
虽然这不是直接回答你的问题,我把这个放在这里,因为谷歌在我搜索这个信息时首先把这个页面放在我的搜索中。
如果您正在使用Visual Studio,您可以右键单击您的项目->属性->构建->高级 这应该列出可用的版本以及您的项目正在使用的版本。