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

import sys
print sys.version

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

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

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

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


当前回答

你所使用的c#版本完全取决于你所使用的。net版本。

如果你使用visual studio进行开发,你可以选择。net框架版本 与之相关的c#版本也随之而来

以下是已知的c#版本:

C# 1.0 released with .NET 1.0 and VS2002 (January 2002) C# 1.2 (bizarrely enough); released with .NET 1.1 and VS2003 (April 2003). First version to call Dispose on IEnumerators which implemented IDisposable. A few other small features. C# 2.0 released with .NET 2.0 and VS2005 (November 2005). Major new features: generics, anonymous methods, nullable types, iterator blocks C# 3.0 released with .NET 3.5 and VS2008 (November 2007). Major new features: lambda expressions, extension methods, expression trees, anonymous types, implicit typing (var), query expressions C# 4.0 released with .NET 4 and VS2010 (April 2010). Major new features: late binding (dynamic), delegate and interface generic variance, more COM support, named arguments and optional parameters C# 5.0 released with .NET 4.5 in August 2012.

参考Jon Skeet的c#版本回答

其他回答

默认情况下,以下是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"

感谢@fortesl和这个答案

我使用的是VS 2019,它不容易告诉你你正在使用的c#版本。我正在使用。net Core 3.0,在那种环境中使用c# 8的VS 2019。但是"csc -langversion:?"说明了这一点:

D:\>csc -langversion:?
Supported language versions:
default
1
2
3
4
5
6
7.0
7.1
7.2
7.3
8.0 (default)
latestmajor
preview
latest

不确定csc -version正在识别什么:

D:\>csc --version
Microsoft (R) Visual C# Compiler version 3.4.0-beta1-19462-14 (2f21c63b)
Copyright (C) Microsoft Corporation. All rights reserved.

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

为了查看已安装的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

如果你正在使用VS2015,那么按照下面的步骤来找到相同的:

右键单击项目。 单击Properties选项卡。 从属性窗口选择“构建”选项。 然后点击Advance按钮。 在那里你会找到语言版本。

下面的图片显示了相同的步骤:

步骤1:

步骤2: