我想知道我使用的是哪个版本的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,您可以右键单击您的项目->属性->构建->高级 这应该列出可用的版本以及您的项目正在使用的版本。
其他回答
默认情况下,以下是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#版本,请使用微软文档中的这段代码来获得. net框架版本,然后使用其他人都提到的表进行匹配。你可以在字典或其他东西中编写框架到c#版本的映射,让你的函数返回c#版本。如果你有。net Framework >= 4.5。
using System;
using Microsoft.Win32;
public class GetDotNetVersion
{
public static void Main()
{
Get45PlusFromRegistry();
}
private static void Get45PlusFromRegistry()
{
const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
{
if (ndpKey != null && ndpKey.GetValue("Release") != null) {
Console.WriteLine($".NET Framework Version: {CheckFor45PlusVersion((int) ndpKey.GetValue("Release"))}");
}
else {
Console.WriteLine(".NET Framework Version 4.5 or later is not detected.");
}
}
// Checking the version using >= enables forward compatibility.
string CheckFor45PlusVersion(int releaseKey)
{
if (releaseKey >= 528040)
return "4.8 or later";
if (releaseKey >= 461808)
return "4.7.2";
if (releaseKey >= 461308)
return "4.7.1";
if (releaseKey >= 460798)
return "4.7";
if (releaseKey >= 394802)
return "4.6.2";
if (releaseKey >= 394254)
return "4.6.1";
if (releaseKey >= 393295)
return "4.6";
if (releaseKey >= 379893)
return "4.5.2";
if (releaseKey >= 378675)
return "4.5.1";
if (releaseKey >= 378389)
return "4.5";
// This code should never execute. A non-null release key should mean
// that 4.5 or later is installed.
return "No 4.5 or later version detected";
}
}
}
// This example displays output like the following:
// .NET Framework Version: 4.6.1
前面的大部分答案都是正确的。只是巩固3,我发现这很简单,超级快。此外,#1现在在VS2019中是不可能的。
Visual Studio:项目>属性>构建>高级 这个选项现在被禁用了,默认语言由VS自己决定。详细的原因可以在这里找到。 在代码中任意位置输入“#error version”(.cs),鼠标悬停。 进入“Visual Studio开发人员命令提示符”并运行以下命令。 csc比如langversion: ?
阅读更多提示:这里。
这里有一个奇怪的技巧,可以快速找到你正在使用的c#版本。编辑项目中的任何。cs文件,并在命名空间语句的末尾添加一个分号:
命名空间Whatever ->命名空间Whatever;
命名空间工作将以红色下划线显示;将鼠标悬停在它上面,它会显示如下内容: “CS8370:文件范围的命名空间在c#7.3中不可用。请使用10.0或更高版本的语言。”
在VS2022上使用。net Framework项目进行测试。如果使用的是。net或。net Core项目,那么这个特性可以很好地工作。
这里概述了. 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