skip to content

LangVersion and Targets

Each target framework defaults to one C# version, LangVersion can raise it, and features that need runtime support fail on old targets. Interviewers ask about C# on .NET Framework.

on this pageshow

explore

questions

5

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.

open as a page

A netstandard2.0 C# library shared by net48 and net10.0 apps needs records and required members; what fails, and how is it fixed?

level: middleimportance: should knowfreq 40%

basics

~10 s

At the default C# 7.3 both are rejected as unavailable syntax. Raising LangVersion exposes missing types instead: IsExternalInit (CS0518) and RequiredMemberAttribute plus CompilerFeatureRequiredAttribute (CS0656), which you declare yourself as internal polyfills.

open as a page

In a C# project file, how do the LangVersion values default, latest, latestMajor, preview and a number differ, and when does the build reject one?

level: seniorimportance: should knowfreq 30%

basics

~20 s

A number pins the language. default, latest and latestMajor all mean the compiler's newest release (C# 14 in the .NET 10 SDK), whatever the target framework; preview adds unreleased features. An unknown value fails with CS1617.

open as a page

After LangVersion 12 is set on a netstandard2.0 C# library, switch expressions compile but a default interface method fails with CS8701; why?

level: seniorimportance: should knowfreq 38%

basics

~20 s

Switch expressions are lowered entirely by the compiler. A default interface method needs the runtime to support it, which the compiler checks through RuntimeFeature.DefaultImplementationsOfInterfaces in the core library; netstandard2.0 lacks it, and LangVersion cannot add it.

open as a page

Your net48 Windows service and netstandard2.0 shared library compile as C# 7.3; would you raise LangVersion above that default, and under what rules?

level: principalimportance: should knowfreq 26%

basics

~20 s

Possibly, as a governed exception: the docs call a C# version above the framework default unsupported. If raised, pin a number on old targets only, allow compiler-only and polyfilled features, keep polyfills internal, and plan the move to modern .NET.

open as a page