我想知道我使用的是哪个版本的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 当前版本,见下文

其他回答

.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 当前版本,见下文

要从代码中获得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中,当构建详细度设置为“normal”或更高(默认值为“minimal”)时,通过查看构建输出来查看c#语言版本。

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

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

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

虽然这不是直接回答你的问题,我把这个放在这里,因为谷歌在我搜索这个信息时首先把这个页面放在我的搜索中。

如果您正在使用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"