Which version of C# am I using

C#Version

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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 released with .NET 4.6.2 > > C# 7.3 released with .NET 4.7.2 > > C# 8.0 released with NET Core 3.0 > > C# 9.0 released with NET 5.0 > > C# 10.0 released with NET 6.0

Solution 2 - C#

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

Solution 3 - C#

To get version of framework - look at version of one of main Assemblies i.e.

 Console.Write(typeof(string).Assembly.ImageRuntimeVersion);

Getting version of C# compiler is somewhat harder, but you should be able to guess version by checking what framework version is used.

If you are using command line compiler (csc.exe) you can check help to see version (also you'd need to know Framework version anyway:

C:\Windows\Microsoft.NET\Framework\v4.0.30319>csc /?
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1

Solution 4 - C#

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)

Solution 5 - C#

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.

Solution 6 - C#

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.

Solution 7 - C#

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

Solution 8 - C#

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.

Solution 9 - C#

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

Solution 10 - C#

.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

Solution 11 - C#

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"

Solution 12 - C#

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.

Solution 13 - C#

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

Solution 14 - C#

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

Solution 15 - C#

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.

Solution 16 - C#

In visual studio 2022 we can check from project properties inside build in Advanced Tab

Screenshot of how to check c# version in visual studio 2022

Solution 17 - C#

In Visual Studio, see the C# language version by looking at the build output when build verbosity is set "normal" or higher (default is "minimal").

  1. Adjust your build output verbosity to "Normal", "Detailed", or "Diagnostic". (The other two values, "Quiet" and "Minimal" do not show the language version.) To set build output verbosity, select menu Tools | Options... and then in the "Options" dialog, select "Project and Solutions" | "Build and Run". Look for the label "MSBuild project output verbosity". Set to one of the three values given, above.

To see the C# language version in use, set build output verbosity.

  1. Build (If your build is up to date, force a build with the menu selection Build | Rebuild.)

  2. Open the Output window Menu selection View | Output.

  3. In the output window, set "Build" for the item labeled "Show output from:"

  4. Search for "langversion". In the middle of a very, very long line (1578 characters on my machine just now) look for a switch like this: /langversion:9.0

(To search in the Output window, click in the Output window then use the keyboard shortcut Ctrl+F to summon the search pane.)

Here is an example that shows the C# language version for my current project.

Solution 18 - C#

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

Solution 19 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSalvador DaliView Question on Stackoverflow
Solution 1 - C#PraveenView Answer on Stackoverflow
Solution 2 - C#DarthView Answer on Stackoverflow
Solution 3 - C#Alexei LevenkovView Answer on Stackoverflow
Solution 4 - C#forteslView Answer on Stackoverflow
Solution 5 - C#Simon TewsiView Answer on Stackoverflow
Solution 6 - C#SanView Answer on Stackoverflow
Solution 7 - C#Sunil JadhavView Answer on Stackoverflow
Solution 8 - C#guyrView Answer on Stackoverflow
Solution 9 - C#Vinay Pratap Singh BhadauriaView Answer on Stackoverflow
Solution 10 - C#Amod PandeyView Answer on Stackoverflow
Solution 11 - C#Ankit MoriView Answer on Stackoverflow
Solution 12 - C#Youssef13View Answer on Stackoverflow
Solution 13 - C#SreekanthView Answer on Stackoverflow
Solution 14 - C#Siyavash HamdiView Answer on Stackoverflow
Solution 15 - C#RomanView Answer on Stackoverflow
Solution 16 - C#Hisham ShahidView Answer on Stackoverflow
Solution 17 - C#Paul YaoView Answer on Stackoverflow
Solution 18 - C#edWView Answer on Stackoverflow
Solution 19 - C#Stack UnderflowView Answer on Stackoverflow