skip to content

In an SDK-style C# project with no LangVersion set, how does the TargetFramework decide which C# language version the compiler uses?

level: juniorimportance: must knowfreq 62%

answer

  1. the SDK, not the compiler, chooses
  2. one C# version per target framework
  3. MaxSupportedLangVersion in the C# targets
  4. .NET Framework and netstandard2.0 stay old
  5. #error version prints it

basics

~10 s

The .NET SDK maps the TargetFramework to a default C# version and passes it as LangVersion: net10.0 gets C# 14, net9.0 C# 13, net8.0 C# 12, while .NET Framework and netstandard2.0 get C# 7.3.

solid answer

~40 s

The compiler in the SDK can compile every version up to its newest (C# 14 in the .NET 10 SDK), but the SDK's `Microsoft.CSharp.Core.targets` computes `MaxSupportedLangVersion` from the target framework and copies it into `LangVersion` when the project leaves it empty. So `net10.0` compiles as C# 14, `net9.0` as C# 13, `net8.0` as C# 12, `net6.0` as C# 10, `netcoreapp3.x` and `netstandard2.1` as C# 8.0, and every .NET Framework target, `netstandard2.0` and older as C# 7.3. The rule is that a C# version is supported only on the .NET version it shipped with and newer, because many features need types or runtime behaviour from that release. To see what a project really compiles with, put `#error version` in a file and read error CS8304.

code

xml · 6 lines
xml
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <!-- no LangVersion: the SDK sets it to 14.0 for net10.0 -->
  </PropertyGroup>
</Project>

go deeper

for a junior

Recall the mapping for the frameworks you meet: net10.0 is C# 14, net8.0 is C# 12, .NET Framework and netstandard2.0 are C# 7.3, and #error version shows the truth.

for a middle

Explain that the SDK's C# targets compute MaxSupportedLangVersion from the framework and only fill LangVersion when the project leaves it empty, and why features need matching library types.

for a senior

Use the mapping to diagnose 'feature not available' errors after an SDK or framework change, and know which projects in a solution compile with which C# version before touching shared code.

for a principal

Treat the framework-to-language link as the supported baseline when planning upgrades: moving target frameworks is the sanctioned way to gain language features, and anything else is a deliberate exception.

## Two versions that people mix up A C# build involves two different version numbers: - the **compiler version**, which is the Roslyn build inside the installed .NET SDK (the .NET 10.0.401 SDK carries compiler 5.9 and knows C# up to **14.0**); - the **language version**, which is the set of C# rules that compiler is told to accept for this project, passed to it as `-langversion` from the MSBuild property `LangVersion`. Installing a newer SDK upgrades the compiler, but it does not make every project compile as the newest C#. The language version comes from the project. ## How the SDK picks the default When the project file does not set `LangVersion`, the SDK fills it in. The logic sits in `Microsoft.CSharp.Core.targets`, which ships with the SDK's Roslyn folder. It computes an internal property `_MaxSupportedLangVersion` from `TargetFrameworkIdentifier` and the framework version, caps it at the compiler's newest version (`_MaxAvailableLangVersion`, 14.0 in this SDK), exposes it as `MaxSupportedLangVersion`, and then sets `LangVersion` to it only if `LangVersion` is still empty. | Target framework | Default C# version | |---|---| | `net10.0` | C# 14 | | `net9.0` | C# 13 | | `net8.0` | C# 12 | | `net7.0` | C# 11 | | `net6.0` | C# 10 | | `net5.0` | C# 9.0 | | `netcoreapp3.x`, `netstandard2.1` | C# 8.0 | | `netcoreapp2.x`, `netstandard2.0` and 1.x, every .NET Framework (`net48`, `net472`, ...) | C# 7.3 | For .NET 5 and later the targets use a formula, 9 plus the major version minus 5, which is why each yearly .NET release lines up with the next C# release. The cap matters too: a target framework newer than the compiler knows cannot raise the default beyond the compiler's newest version. ## Why the default is tied to the framework The C# docs state the reason plainly: the compiler generates code that relies on types and members in the standard library, so **a C# version is supported only on the matching .NET version and newer**. Some features are pure syntax, but others need a library type (init-only setters need `IsExternalInit`, index expressions need `System.Index`) or support in the runtime itself (default interface members, static abstract interface members). Pinning the language to the framework guarantees that everything the compiler accepts will also work when the program runs. This is also why .NET Framework projects are stuck at C# 7.3 by default: the 4.8.x line is the final .NET Framework, and it never gained the runtime features later C# versions depend on. ## Checking what a project really uses Three ways to see the effective language version: 1. Add a line `#error version` to any source file and build. The compiler fails with **CS8304**, whose message names the compiler version and the language version in use. 2. Ask MSBuild for the property: `dotnet msbuild -getProperty:LangVersion`. This shows the value MSBuild passes, which can be a keyword rather than a number when someone set one. 3. Read a detailed build log (`-v:d`) for the `/langversion:` switch passed to the compiler. ## What the language version does not decide - It does not choose the APIs you can call: those come from the target framework's reference assemblies, so C# 14 syntax never adds a method that `net8.0` lacks. - It does not choose the runtime the program runs on; the target framework and the deployment do. - It does not pin the compiler: a project at C# 12 built with the .NET 10 SDK still runs through the newer compiler, only with C# 12's language rules. ## Changing it The normal way to get a newer C# is to move the project to a newer target framework; the language version follows. Setting `LangVersion` by hand is possible, and it is what this leaf's other questions explore, but the docs mark a language version newer than the framework's default as **not supported**. Lowering it (for example `LangVersion` 10 on a `net10.0` project) is allowed and sometimes used to keep a codebase compiling on an older toolchain. ## Common misunderstandings - The newest SDK does not mean the newest C# for every project; a `net8.0` project built with the .NET 10 SDK still compiles as C# 12. - `dotnet --version` reports the SDK, not the language version. - `netstandard2.1` gets C# 8.0, not 7.3; only `netstandard2.0` and earlier fall back to 7.3.

  • A colleague installs the .NET 10 SDK and expects their net8.0 project to accept C# 14 syntax. What happens?
    The project still compiles as C# 12. The new compiler knows C# 14, but the SDK's C# targets set `LangVersion` from the target framework, and `net8.0` maps to C# 12. Using a C# 13 or 14 feature fails with a 'feature is not available in C# 12' error until the target framework moves to `net9.0` or `net10.0`, or someone sets `LangVersion` explicitly.
  • Why does a netstandard2.1 library get a newer default than a netstandard2.0 one?
    The targets treat `netstandard2.1` like `netcoreapp3.x` and give it C# 8.0, because .NET Standard 2.1 includes types C# 8 relies on, such as `System.Index` and `System.Range`. `netstandard2.0` is implemented by .NET Framework too, which lacks those types and the runtime support for C# 8's default interface members, so it stays at C# 7.3.

  • Installing a newer SDK upgrades every project to the newest C#
  • dotnet --version tells you which C# a project uses
  • netstandard2.0 defaults to C# 8 like netstandard2.1
  • The compiler, not the SDK targets, picks the default per framework
  • Setting LangVersion by hand is the normal way to get newer C#

Assumes the .NET 10.0.401 SDK, whose compiler supports up to C# 14 and maps net10.0 to C# 14.