C# language versioning
The latest C# compiler determines a default language version based on your project's target framework or frameworks. Visual Studio doesn't provide a UI to change the value, but you can change it by editing the csproj file. The choice of default ensures that you use the latest language version compatible with your target framework. You benefit from access to the latest language features compatible with your project's target. This default choice also ensures you don't use a language that requires types or runtime behavior not available in your target framework. Choosing a language version newer than the default can cause hard to diagnose compile-time and runtime errors.
The rules in this article apply to the compiler delivered with Visual Studio 2019 or the .NET Core 3.0 SDK. The C# compilers that are part of the Visual Studio 2017 installation or earlier .NET Core SDK versions target C# 7.0 by default.
C# 8.0 (and higher) is supported only on .NET Core 3.x and newer versions. Many of the newest features require library and runtime features introduced in .NET Core 3.x:
- Default interface implementation requires new features in the .NET Core 3.0 CLR.
- Async streams require the new types System.IAsyncDisposable, System.Collections.Generic.IAsyncEnumerable<T>, and System.Collections.Generic.IAsyncEnumerator<T>.
- Indices and ranges require the new types System.Index and System.Range.
- Nullable reference types make use of several attributes to provide better warnings. Those attributes were added in .NET Core 3.0. Other target frameworks haven't been annotated with any of these attributes. That means nullable warnings may not accurately reflect potential issues.
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|
When your project targets a preview framework that has a corresponding preview language version, the language version used is the preview language version. You use the latest features with that preview in any environment, without affecting projects that target a released .NET Core version.
Override a default
If you must specify your C# version explicitly, you can do so in several ways:
- Manually edit your project file.
- Set the language version for multiple projects in a subdirectory.
- Configure the
Edit the project file
You can set the language version in your project file. For example, if you explicitly want access to preview features, add an element like this:
<PropertyGroup> <LangVersion>preview</LangVersion> </PropertyGroup>
preview uses the latest available preview C# language version that your compiler supports.
Configure multiple projects
To configure multiple projects, you can create a Directory.Build.props file that contains the
<LangVersion> element. You typically do that in your solution directory. Add the following to a Directory.Build.props file in your solution directory:
<Project> <PropertyGroup> <LangVersion>preview</LangVersion> </PropertyGroup> </Project>
Builds in all subdirectories of the directory containing that file will use the preview C# version. For more information, see the article on Customize your build.
C# language version reference
The following table shows all current C# language versions. Your compiler may not necessarily understand every value if it's older. If you install .NET Core 3.0 or later, then you have access to everything listed.
||The compiler accepts all valid language syntax from the latest preview version.|
||The compiler accepts syntax from the latest released version of the compiler (including minor version).|
||The compiler accepts syntax from the latest released major version of the compiler.|
||The compiler accepts only syntax that is included in C# 8.0 or lower.|
||The compiler accepts only syntax that is included in C# 7.3 or lower.|
||The compiler accepts only syntax that is included in C# 7.2 or lower.|
||The compiler accepts only syntax that is included in C# 7.1 or lower.|
||The compiler accepts only syntax that is included in C# 7.0 or lower.|
||The compiler accepts only syntax that is included in C# 6.0 or lower.|
||The compiler accepts only syntax that is included in C# 5.0 or lower.|
||The compiler accepts only syntax that is included in C# 4.0 or lower.|
||The compiler accepts only syntax that is included in C# 3.0 or lower.|
||The compiler accepts only syntax that is included in ISO/IEC 23270:2006 C# (2.0).|
||The compiler accepts only syntax that is included in ISO/IEC 23270:2003 C# (1.0/1.2).|
Open the Developer Command Prompt for Visual Studio, and run the following command to see the listing of language versions available on your machine.
Questioning the -langversion compile option like this, will print something similar to the following:
Supported language versions: default 1 2 3 4 5 6 7.0 7.1 7.2 7.3 8.0 (default) latestmajor preview latest