Reputation: 222879
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.
Upvotes: 384
Views: 342773
Reputation: 56539
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
C# 11.0 released with NET 7.0
C# 12.0 released with NET 8.0
Upvotes: 432
Reputation: 100610
You can't find out what language C# version was used by compiler to produce the assembly and hence there is no code you can write to detect it at run-time.
At compile time C# version is defined by setting in the project file for project (can be viewd in VS settings of the project as shown in another answer) and explicit or implicit switch passed to compiler if compiling directly with CSC (How do I tell the Roslyn C# compiler to use a specific version of .NET?)
The only version you can get at run-time is the version of the 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 of the compiler itself (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
Note that each version of the compiler can target all previous versions of C# - following Roslyn compiler command reporting supported C# versions 1-11:
"C:\Program Files\Microsoft Visual
Studio\2022\Preview\MSBuild\Current\Bin\Roslyn\csc.exe" /langversion:?
Upvotes: 101
Reputation: 301
Just write #error version in your .cs file and look at error list as shown below indicating your language version. Hope it helps.
Upvotes: 12
Reputation: 37
Here is one weird trick to quickly find what version of C# you are using. Edit any .cs file in your project and add a semicolon at the end on the namespace statement:
namespace Whatever -> namespace Whatever;
The namespace work will become underlined in red; hover you mouse over it and it will say something like the following: "CS8370: File-scoped namespace is not available in c#7.3. Please use language version 10.0 or greater."
Tested on VS2022 using a .NET Framework project. If using a .NET or .NET Core project then this feature will work fine.
Upvotes: 1
Reputation: 537
Most of the previous answers are correct. Just consolidating 3 which I found easy and super quick. Also, #1 is not possible now in VS2019.
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.
Type "#error version" in the code(.cs) anywhere, mousehover it.
Go to 'Developer Command Prompt for Visual Studio' and run following command.
csc -langversion:?
Read more tips: here.
Upvotes: 42
Reputation: 303
In visual studio 2022 we can check from project properties inside build in Advanced Tab
Upvotes: 11
Reputation: 681
From developer command prompt ( Visual Studio > View > Terminal ) 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)
Upvotes: 46
Reputation: 310
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").
Build (If your build is up to date, force a build with the menu selection Build | Rebuild.)
Open the Output window Menu selection View | Output.
In the output window, set "Build" for the item labeled "Show output from:"
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.)
Upvotes: 3
Reputation: 19788
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.
Upvotes: 82
Reputation: 4986
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.
Upvotes: 8
Reputation: 395
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
Upvotes: 7
Reputation: 2453
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
Upvotes: 0
Reputation: 1465
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.
Upvotes: 143
Reputation: 2303
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:
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
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
Upvotes: 1
Reputation: 166
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.
Upvotes: 11
Reputation: 127
If you are using VS2015 then follow below steps to find out the same:
Below images show the steps for the same:
Step 1:
Step 2:
Upvotes: 11
Reputation: 21865
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.
Upvotes: 3
Reputation: 775
By default following are corresponding version of C# compilers for Visual Studio:
You can also modify version please follow below steps.
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"
Upvotes: 8
Reputation: 9947
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
onIEnumerator
s which implementedIDisposable
. 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
Upvotes: 11
Reputation: 3087
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
Upvotes: 4
Reputation: 1396
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
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
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
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
Upvotes: 10