[c#] Which version of C# am I using

I want to find out which version of C# I'm using. If I would be using python I would do something like python -V from the command line, or type:

import sys
print sys.version

In PHP I would do something like this: phpinfo(); in java: java -version

But I was not able to find how to achieve this in C#.

This question does not answer it, although the name suggests that it should.

I got that it depends on the .NET framework, but is there a programmatic way of figuring out my framework? I mean without going to the directory and checking the name of my .NET folders.

This question is related to c# version

The answer is


By default following are corresponding version of C# compilers for Visual Studio:

  1. Visual Studio 2015: C# 6.0
  2. Visual Studio 2013: C# 5.0
  3. Visual Studio 2012: C# 5.0
  4. Visual Studio 2010: C# 4.0
  5. Visual Studio 2008: C# 3.0
  6. Visual Studio 2005: C# 2.0
  7. Visual Studio.NET 2003: C# 1.2
  8. Visual Studio.NET 2002: C# 1.0

You can also modify version please follow below steps.

Open the project properties window:

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"

The language version is chosen based on the project's target framework by default.

Each project may use a different version of .Net framework, the best suitable C# compiler will be chosen by default by looking at the target framework. From visual studio, UI will not allow the users to changes the language version, however, we can change the language version by editing the project file with addition of new property group. But this may cause compile/run time issues in existing code.

<PropertyGroup>  
<LangVersion>8.0</LangVersion>  
</PropertyGroup>

I could see the following from Microsoft docs.

The compiler determines a default based on these rules:

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

Most of the answers are correct above. Just consolidating 3 which I found easy and super quick. Also #1 is not possible now in VS2019.

  1. Visual Studio: Project > Properties > Build > Advanced
    This option is disabled now, and the default language is decided by VS itself. The detailed reason is available here.

  2. Type "#error version" in the code(.cs) anywhere, mousehover it.

  3. Go to 'Developer Command Prompt for Visual Studio' and run following command. csc -langversion:?

Read more tips: here.


The C# version you are using totally depends upon the .Net version you are using.

if you are using visual studio for development, you get to choose the .net framework version the c# version associated with it comes accordingly

These are the versions of C# known:

  • 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.

Refrence Jon Skeet's C# Versions Answer


While this isn't answering your question directly, I'm putting this here as google brought this page up first in my searches when I was looking for this info.

If you're using Visual Studio, you can right click on your project -> Properties -> Build -> Advanced This should list available versions as well as the one your proj is using.

enter image description here


To get the C# version from code, use this code from the Microsoft documentation to get the .NET Framework version and then match it up using the table that everyone else mentions. You can code up the Framework to C# version map in a dictionary or something to actually have your function return the C# version. Works if you have .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

For Windows, you run dev at the command/ search program line and select Developer Command Prompt for VS. Then you are going to just run

csc

Now you get information similar to

Microsoft (R) Visual C# Compiler version 2.6.0.62329 (5429b35d)
Copyright (C) Microsoft Corporation. All rights reserved.   

For Windows and if you start with cmd terminal

cd C:\Windows\Microsoft.NET\Framework\
dir

Now you see all directories and files in .NET\Framework\ Please, select v... latest and go there, for example,

cd v4.0.30319

Run

csc

You will see information about version of C# compiler, that can be something similar to

Microsoft (R) Visual C# Compiler version 4.7.2556.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

From developer command prompt type

csc -langversion:?

That will display all C# versions supported including the default:

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

Thanks to @fortesl and this answer

I'm using VS 2019 and it doesn't easily tell you the C# version you are using. I'm working with .Net Core 3.0, and VS 2019 using C# 8 in that environment. But "csc -langversion:?" makes this clear:

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

Not sure what csc -version is identifying:

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

.NET version through registry

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\ explore the children and look into each version. The one with key 'Full' is the version on the system.

https://support.microsoft.com/en-us/kb/318785 https://msdn.microsoft.com/en-us/library/hh925568(v=vs.110).aspx

.NET version through Visual Studio

Help -> About Microsoft Visual Studio -> The .NET version is specified on the top right.

As I understand at this time the Visual studio uses .NET Framework from the OS.

The target .NET Framework version of a project in Visual Studio can be modified with Project Properties -> Application -> Target Framework

Through the dll

If you know the .NET Framework directory e.g. C:\Windows\Microsoft.NET\Framework64\v4.0.30319

Open System.dll, right click -> properties -> Details tab

C# version

Help -> About Microsoft Visual Studio

In the installed products lists there is Visual C#. In my case Visual C# 2015

Visual Studio (Microsoft) ships C# by name Visual C#.

https://msdn.microsoft.com/en-us/library/hh156499.aspx

C# 6, Visual Studio .NET 2015 Current version, see below


Useful tip for finding the C# version:

To know what language version you're currently using, put #error version (case sensitive) in your code. This makes the compiler produce a diagnostic, CS8304, with a message containing the compiler version being used and the current selected language version.

From C# language versioning in MS docs.

You can drop #error version anywhere in your code. It will generate an error so you'll need to remove it after you've used it. The error gives details of the compiler and language versions, eg:

Error   CS8304  Compiler version: '3.7.0-6.20459.4 (7ee7c540)'. Language version: 8.0.

In VS 2019, at least, you don't need to compile your code for #error version to give you the version number. Just mousing over it in your code will bring up the same error details you would see when you compile the code.


Here is an overview of how the .NET framework and compiler versions are related, set and modified. Each project has a target .NET framework version(s), for example version 3.x or 2.x . The .NET framework contains the run time types and components.

The Visual Studio version installation and the .NET framework version determine the compatible c# language version and compiler options that can be used. The default c# version and options used in a Visual Studio project is the latest language version installed that is compatible with the .NET framework version being used.

To view or update the Framework or C# language within a project within Visual Studio 2011:

  • 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.

To see the exact compiler language version for 'default', enter the following in the developer command prompt for your installed Visual Studio version. For example, from the Windows Start icon select icon: "Developer Command Prompt for VS2011' and enter:

csc -langversion:Default

Microsoft (R) Visual C# Compiler version 4.7.3062.0 for c# 5


It depends upon the .NET Framework that you use. Check Jon Skeet's answer about Versions.

Here is short version of his answer.

C# 1.0 released with .NET 1.0

C# 1.2 (bizarrely enough); released with .NET 1.1

C# 2.0 released with .NET 2.0

C# 3.0 released with .NET 3.5

C# 4.0 released with .NET 4

C# 5.0 released with .NET 4.5

C# 6.0 released with .NET 4.6

C# 7.0 is released with .NET 4.6.2

C# 7.3 is released with .NET 4.7.2

C# 8.0 is released with NET Core 3.0

C# 9.0 is released with NET 5.0


In order to see the installed compiler version of VC#:

Open Visual Studio command prompt and just type csc then press Enter.

You will see something like following:

Microsoft (R) Visual C# Compiler version 4.0.30319.34209

for Microsoft (R) .NET Framework 4.5

Copyright (C) Microsoft Corporation. All rights reserved.

P.S.: "CSC" stand for "C Sharp Compiler". Actually using this command you run csc.exe which is an executable file which is located in "c:\Windows\Microsoft.NET\Framework\vX.X.XXX". For more information about CSC visit http://www.file.net/process/csc.exe.html


The current answers are outdated. You should be able to use #error version (at the top of any C# file in the project, or nearly anywhere in the code). The compiler treats this in a special way and reports a compiler error, CS8304, indicating the language version, and also the compiler version. The message of CS8304 is something that looks like the following:

error CS8304: Compiler version: '3.7.0-3.20312.3 (ec484126)'. Language version: 6.


If you are using VS2015 then follow below steps to find out the same:

  1. Right click on the project.
  2. Click on the Properties tab.
  3. From properties window select Build option.
  4. In that click on the Advance button.
  5. There you will find out the language version.

Below images show the steps for the same:

Step 1:

Step 1

Step 2:

Step 2