Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Configure C# language version

Feedback

In this article

The information in this article applies to .NET 5 and later. For UWP projects, see the article onChoosing a UWP version.

The C# language reference documents the most recently released version of the C# language. It also contains initial documentation for features in public previews for the upcoming language release.

The documentation identifies any feature first introduced in the last three versions of the language or in current public previews.

Tip

To find when a feature was first introduced in C#, consult the article on theC# language version history.

In Visual Studio, the option to change the language version through the UI is disabled because the default version aligns with the project's target framework (TFM). This default configuration ensures compatibility between language features and runtime support. To change the language version in Visual Studio, change the project's target framework.

For example, changing the targetTFM (for example, from.NET 6 to.NET 9) updates the language version accordingly, from C# 10 to C# 13. This approach prevents issues with runtime compatibility and minimizes unexpected build errors due to unsupported language features.

If you need a specific language version that differs from the one automatically selected, use the methods in this article to override the default settings directly in the project file.

Warning

Don't set theLangVersion element tolatest. Thelatest setting means the installed compiler uses its latest version. The value oflatest can change from machine to machine, making builds unreliable. In addition, it enables language features that might require runtime or library features not included in the current SDK.

If you must specify your C# version explicitly, you can do so in several ways:

Tip

You can see the language version in Visual Studio in the project properties page. Under theBuild tab, theAdvanced pane displays the version selected.

To know what language version you're currently using, put#error version (case sensitive) in your code. This pragma makes the compiler report a compiler error, CS8304, with a message containing the compiler version and the current selected language version. For more information about this pragma, see#error (C# Reference).

Edit the project file

Set the language version in your project file. The project file is the*.csproj file in the root folder for your project. For example, if you want explicit access to preview features, add an element like the following example:

<PropertyGroup>   <LangVersion>preview</LangVersion></PropertyGroup>

The valuepreview uses the latest available preview C# language version your compiler supports.

Configure multiple projects

To configure multiple C# projects, create aDirectory.Build.props file, typically in your solution directory, that contains the<LangVersion> element. Add the following setting to theDirectory.Build.props file:

<Project> <PropertyGroup>   <LangVersion>preview</LangVersion> </PropertyGroup></Project>

Builds in all subdirectories of the directory containing that file now use the preview C# version. For more information, seeCustomize your build.

Note

The versions for C# and Visual Basic are different. Don't use theDirectory.Build.Props file for a folder where subdirectories contain projects for both languages. The versions don't match.

C# language version reference

Important

Using a C# language version newer than the version associated with your target TFM isn't supported.

The following table shows all current C# language versions. Older compilers might not understand every value. If you install the latest .NET SDK, you have access to everything listed.

ValueMeaning
previewThe compiler accepts all valid language syntax from the latest preview version.
latestThe compiler accepts syntax from the latest released version of the compiler (including minor version).
latestMajor
ordefault
The compiler accepts syntax from the latest released major version of the compiler.
15.0The compiler accepts only syntax that is included in C# 15 or lower.
14.0The compiler accepts only syntax that is included in C# 14 or lower.
13.0The compiler accepts only syntax that is included in C# 13 or lower.
12.0The compiler accepts only syntax that is included in C# 12 or lower.
11.0The compiler accepts only syntax that is included in C# 11 or lower.
10.0The compiler accepts only syntax that is included in C# 10 or lower.
9.0The compiler accepts only syntax that is included in C# 9 or lower.
8.0The compiler accepts only syntax that is included in C# 8.0 or lower.
7.3The compiler accepts only syntax that is included in C# 7.3 or lower.
7.2The compiler accepts only syntax that is included in C# 7.2 or lower.
7.1The compiler accepts only syntax that is included in C# 7.1 or lower.
7The compiler accepts only syntax that is included in C# 7.0 or lower.
6The compiler accepts only syntax that is included in C# 6.0 or lower.
5The compiler accepts only syntax that is included in C# 5.0 or lower.
4The compiler accepts only syntax that is included in C# 4.0 or lower.
3The compiler accepts only syntax that is included in C# 3.0 or lower.
ISO-2
or2
The compiler accepts only syntax that is included in ISO/IEC 23270:2006 C# (2.0).
ISO-1
or1
The compiler accepts only syntax that is included in ISO/IEC 23270:2003 C# (1.0/1.2).
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?