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

import sys
print sys.version

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

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

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

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


当前回答

默认情况下,基于项目的目标框架选择语言版本。

每个项目可能使用不同版本的。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

其他回答

在Visual Studio中,当构建详细度设置为“normal”或更高(默认值为“minimal”)时,通过查看构建输出来查看c#语言版本。

将构建输出的详细信息调整为“正常”、“详细”或“诊断”。(另外两个值“Quiet”和“Minimal”不显示语言版本。)要设置构建输出的详细信息,选择菜单Tools | Options…然后在“选项”对话框中,选择“项目和解决方案”|“构建和运行”。寻找标签“MSBuild项目输出冗长”。设置为上面给出的三个值之一。

构建(如果您的构建是最新的,则通过菜单选择Build | Rebuild强制进行构建。) 打开输出窗口 菜单选择查看|输出。 在输出窗口中,为标签为“Show output from”的项目设置“Build”: 搜索“langversion”。在一个非常非常长的行(我的机器上现在有1578个字符)中间,寻找像这样的开关:/langversion:9.0

(要在输出窗口中搜索,在输出窗口中单击,然后使用键盘快捷键Ctrl+F调用搜索窗格。)

为了查看已安装的vc#编译器版本:

打开Visual Studio命令提示符,输入csc,然后按Enter。

你会看到如下内容:

微软(R) Visual c#编译器4.0.30319.34209 用于Microsoft (R) .NET Framework 4.5 微软公司版权所有。版权所有。

附注:“CSC”代表“C夏普编译器”。实际上,使用此命令您可以运行csc.exe,这是一个可执行文件,位于“c:\Windows\Microsoft.NET\Framework\vX.X.XXX”。有关CSC的更多信息,请访问http://www.file.net/process/csc.exe.html

这里概述了. 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

要从代码中获得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

从开发人员命令提示符(Visual Studio > View > Terminal)输入:

csc -langversion:?

将显示所有支持的c#版本,包括默认版本:

1
2
3
4
5
6
7.0 (default)
7.1
7.2
7.3 (latest)