Glossary / C# language versions

C# language versions

C# language versions define which syntax and compiler features are available, while the .NET SDK supplies the compiler and build targets that implement them. A project can target an older runtime and use newer language syntax when the compiler can emit compatible code, but some features require runtime or library support and cannot be enabled by syntax version alone.

Selecting a language version

The SDK normally chooses a default C# version based on the target framework and SDK. LangVersion can override it, but Microsoft advises against latest for reproducible builds because the chosen version can vary by installed compiler. Use a stable SDK and, when needed, an explicit supported language version. Preview versions are for experimentation and may change.

Language features have different dependencies: records and pattern syntax are mostly compiler features; default interface methods and static abstract interface members require supporting runtime capabilities; APIs used by new syntax may require reference assemblies or packages. Target framework, runtime, compiler, and language version are related but distinct axes.

Compatibility and migration

Check the C# version history for feature introduction and required runtime support. Library authors should consider consumer compiler requirements and generated metadata. Multi-targeting can conditionally use newer APIs, while preprocessor symbols can select target-specific implementations. Avoid setting a language version higher than the installed SDK/compiler supports.

Interview questions

  1. Does netX.0 uniquely determine C# syntax? It influences defaults, but SDK/compiler and explicit settings matter.
  2. Can modern C# target an older runtime? Often for compiler-only features, but runtime-dependent features/APIs may not work.
  3. Why avoid LangVersion=latest? Builds can vary across environments with different compiler releases.
  4. What is preview language mode? Access to prerelease features that may change before stable release.
  5. How check feature support? Consult version history and verify compiler, runtime, and reference API requirements.

References