我想知道我使用的是哪个版本的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文件夹的名称。
当前回答
对于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.
其他回答
在visual studio 2022中,我们可以在高级选项卡中查看项目属性
从开发人员命令提示符(Visual Studio > View > Terminal)输入:
csc -langversion:?
将显示所有支持的c#版本,包括默认版本:
1
2
3
4
5
6
7.0 (default)
7.1
7.2
7.3 (latest)
前面的大部分答案都是正确的。只是巩固3,我发现这很简单,超级快。此外,#1现在在VS2019中是不可能的。
Visual Studio:项目>属性>构建>高级 这个选项现在被禁用了,默认语言由VS自己决定。详细的原因可以在这里找到。 在代码中任意位置输入“#error version”(.cs),鼠标悬停。 进入“Visual Studio开发人员命令提示符”并运行以下命令。 csc比如langversion: ?
阅读更多提示:这里。
要从代码中获得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
对于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.