You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/compiler-options/language.md
+15-9Lines changed: 15 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
description: "C# Compiler Options for language feature rules. These options control how the compiler interprets certain language constructs."
3
3
title: "C# Compiler Options - language feature rules"
4
-
ms.date: 07/06/2021
4
+
ms.date: 10/30/2023
5
5
f1_keywords:
6
6
- "cs.build.options"
7
7
helpviewer_keywords:
@@ -60,7 +60,9 @@ This option specifies the names of one or more symbols that you want to define.
60
60
61
61
## LangVersion
62
62
63
-
Causes the compiler to accept only syntax that is included in the chosen C# language specification.
63
+
The default language version for the C# compiler depends on the target framework for your application and the version of the SDK or Visual Studio installed. Those rules are defined in [C# language versioning](../configure-language-version.md#defaults).
64
+
65
+
The **LangVersion** option causes the compiler to accept only syntax that is included in the specified C# language specification, for example:
64
66
65
67
```xml
66
68
<LangVersion>9.0</LangVersion>
@@ -70,18 +72,22 @@ The following values are valid:
The default language version depends on the target framework for your application and the version of the SDK or Visual Studio installed. Those rules are defined in [C# language versioning](../configure-language-version.md#defaults).
74
-
75
75
> [!IMPORTANT]
76
-
> The `latest` value is generally not recommended. With it, the compiler enables the latest features, even if those features depend on updates not included in the configured target framework. Without this setting, your project uses the compiler version recommended for your target framework. You can update the target framework to access newer language features.
76
+
> The `latest` value is generally not recommended. With `latest`, the compiler enables the latest features, even if those features depend on updates not included in the configured target framework.
77
+
78
+
### Considerations
79
+
80
+
- To ensure that your project uses the default compiler version recommended for your target framework, don't use the **LangVersion** option. You can update the target framework to access newer language features.
81
+
82
+
- Specifying **LangVersion** with the `default` value is different from omitting the **LangVersion** option. Specifying `default` uses the latest version of the language that the compiler supports, without taking into account the target framework. For example, building a project that targets .NET 6 from Visual Studio version 17.6 uses C# 10 if **LangVersion** isn't specified, but uses C# 11 if **LangVersion** is set to `default`.
77
83
78
-
Metadata referenced by your C# application isn't subject to the **LangVersion** compiler option.
84
+
-Metadata referenced by your C# application isn't subject to the **LangVersion** compiler option.
79
85
80
-
Because each version of the C# compiler contains extensions to the language specification, **LangVersion** doesn't give you the equivalent functionality of an earlier version of the compiler.
86
+
-Because each version of the C# compiler contains extensions to the language specification, **LangVersion** doesn't give you the equivalent functionality of an earlier version of the compiler.
81
87
82
-
Additionally, while C# version updates generally coincide with major .NET releases, the new syntax and features aren't necessarily tied to that specific framework version. Each specific feature has its own minimum .NET API or common language runtime requirements that may allow it to run on downlevel frameworks by including NuGet packages or other libraries.
88
+
- While C# version updates generally coincide with major .NET releases, the new syntax and features aren't necessarily tied to that specific framework version. Each specific feature has its own minimum .NET API or common language runtime requirements that may allow it to run on downlevel frameworks by including NuGet packages or other libraries.
83
89
84
-
Regardless of which **LangVersion** setting you use, use the current version of the common language runtime to create your .exe or .dll. One exception is friend assemblies and [**ModuleAssemblyName**](advanced.md#moduleassemblyname), which work under **-langversion:ISO-1**.
90
+
-Regardless of which **LangVersion** setting you use, use the current version of the common language runtime to create your *.exe* or *.dll*. One exception is friend assemblies and [ModuleAssemblyName](advanced.md#moduleassemblyname), which work under **-langversion:ISO-1**.
85
91
86
92
For other ways to specify the C# language version, see [C# language versioning](../configure-language-version.md).
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/configure-language-version.md
+11-8Lines changed: 11 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
title: C# language versioning - C# Guide
3
3
description: Learn about how the C# language version is determined based on your project and the reasons behind that choice. Learn how to override the default manually.
4
4
ms.custom: "updateeachrelease"
5
-
ms.date: 02/25/2022
5
+
ms.date: 10/30/2023
6
6
---
7
7
8
8
# C# language versioning
@@ -19,18 +19,18 @@ The compiler determines a default based on these rules:
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.
22
+
If 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.
23
23
24
24
> [!IMPORTANT]
25
-
> The new project template for Visual Studio 2017 added a `<LangVersion>latest</LangVersion>` entry to new project files. If you upgrade the target framework for these projects, they [override the default behavior](#override-a-default). You should remove the `<LangVersion>latest</LangVersion>` from your project file when you update the .NET SDK. Then, your project will use the compiler version recommended for your target framework. You can update the target framework to access newer language features.
25
+
> The new project template for Visual Studio 2017 added a `<LangVersion>latest</LangVersion>` entry to new project files. If you upgrade the target framework for these projects, the `<LangVersion>` setting can [override the default](#override-the-default) for the new target framework. Be sure to remove the `<LangVersion>latest</LangVersion>` from your project file to ensure your project uses the recommended compiler version for your target framework. You can update the target framework to access newer language features.
26
26
27
-
## Override a default
27
+
## Override the default
28
28
29
29
If you must specify your C# version explicitly, you can do so in several ways:
30
30
31
31
- Manually edit your [project file](#edit-the-project-file).
32
32
- Set the language version [for multiple projects in a subdirectory](#configure-multiple-projects).
33
-
- Configure the [**LangVersion** compiler option](compiler-options/language.md#langversion).
33
+
- Configure the [LangVersion compiler option](compiler-options/language.md#langversion).
34
34
35
35
> [!TIP]
36
36
> You can see the language version in Visual Studio in the project properties page. Under the *Build* tab, the *Advanced* pane displays the version selected.
@@ -51,7 +51,7 @@ The value `preview` uses the latest available preview C# language version that y
51
51
52
52
### Configure multiple projects
53
53
54
-
To configure multiple projects, you can create a **Directory.Build.props** filethat 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:
54
+
To configure multiple projects, you can create a *Directory.Build.props* file, typically in your solution directory, that contains the `<LangVersion>` element. Add the following setting to the *Directory.Build.props* file:
55
55
56
56
```xml
57
57
<Project>
@@ -61,10 +61,13 @@ To configure multiple projects, you can create a **Directory.Build.props** file
61
61
</Project>
62
62
```
63
63
64
-
Builds in all subdirectories of the directory containing that file will use the preview C# version. For more information, see [Customize your build](/visualstudio/msbuild/customize-your-build).
64
+
Builds in all subdirectories of the directory containing that file now use the preview C# version. For more information, see [Customize your build](/visualstudio/msbuild/customize-your-build).
65
65
66
66
## C# language version reference
67
67
68
-
The following table shows all current C# language versions. Your compiler may not necessarily understand every value if it's older. If you install the latest .NET SDK, then you have access to everything listed.
68
+
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.
>Specifying **LangVersion** with the `default` value is different from omitting the **LangVersion** option. Specifying `default` uses the latest version of the language that the compiler supports, without taking into account the target framework. For example, building a project that targets .NET 6 from the current version of Visual Studio 2022 uses C# 10 if **LangVersion** isn't specified, but uses C# 11 if **LangVersion** is set to `default`.
0 commit comments