我想知道我使用的是哪个版本的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文件夹的名称。
当前回答
.NET版本通过注册表
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\探索子程序并查看每个版本。键为'Full'的是系统上的版本。
https://support.microsoft.com/en-us/kb/318785 https://msdn.microsoft.com/en-us/library/hh925568 (v = vs.110) . aspx
.NET版本通过Visual Studio
Help ->关于Microsoft Visual Studio -> . net版本在右上角指定。
据我所知,Visual studio在操作系统中使用。net框架。
Visual Studio中项目的目标.NET框架版本可以通过项目属性->应用程序->目标框架来修改
通过dll
如果你知道。net Framework目录 例如C:\Windows\ Microsoft.NET \ Framework64 \ v4.0.30319
打开System.dll,右键单击->属性->详细信息选项卡
c#版本
帮助->关于Microsoft Visual Studio
在已安装的产品列表中有Visual c#。在我的案例中是Visual c# 2015
Visual Studio(微软)将c#命名为Visual c#。
https://msdn.microsoft.com/en-us/library/hh156499.aspx
c# 6, Visual Studio .NET 2015 当前版本,见下文
其他回答
对于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发布
默认情况下,基于项目的目标框架选择语言版本。
每个项目可能使用不同版本的。net框架,最好的c#编译器将通过查看目标框架默认选择。在visual studio中,UI不允许用户更改语言版本,但是,我们可以通过添加新的属性组编辑项目文件来更改语言版本。但这可能会导致现有代码的编译/运行时问题。
<PropertyGroup>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
我可以从微软文档中看到以下内容。
编译器根据以下规则确定默认值:
Target framework version C# language version default
.NET Core 3.x C# 8.0
.NET Core 2.x C# 7.3
.NET Standard 2.1 C# 8.0
.NET Standard 2.0 C# 7.3
.NET Standard 1.x C# 7.3
.NET Framework all C# 7.3
要从代码中获得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
如果你正在使用VS2015,那么按照下面的步骤来找到相同的:
右键单击项目。 单击Properties选项卡。 从属性窗口选择“构建”选项。 然后点击Advance按钮。 在那里你会找到语言版本。
下面的图片显示了相同的步骤:
步骤1:
步骤2: