After LangVersion 12 is set on a netstandard2.0 C# library, switch expressions compile but a default interface method fails with CS8701; why?
answer
- three kinds of features
- syntax, library type, runtime support
- RuntimeFeature in the core library
- no polyfill for the runtime
basics
~20 sSwitch 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.
solid answer
~40 sC# features fall into three groups. Compiler-only features, such as switch expressions, pattern matching and file-scoped namespaces, become ordinary IL, so a higher `LangVersion` is all they need. Library-dependent features need a type the compiler calls or marks with: `init` needs `IsExternalInit`, `values[^1]` needs `System.Index` (CS0518), and those can be supplied by declaring the type. Runtime-dependent features need the CLR to load or dispatch something new: default interface members (CS8701), static abstract interface members (CS8919), ref fields (CS9064), inline arrays (CS9171), covariant returns (CS8830). Roslyn decides by looking for the matching field on `System.Runtime.CompilerServices.RuntimeFeature` in the referenced core library; `netstandard2.0` has none, and declaring your own `RuntimeFeature` does not count. The only fix is a newer target framework or a different design.
code
csharp · 27 linesnamespace Shared;
public static class Fees
{
// Compiler-only: lowers to type tests and branches
public static decimal For(object payment) => payment switch
{
decimal amount when amount > 1000m => amount * 0.01m,
decimal => 2.50m,
_ => 0m,
};
// Needs the System.Index type, which netstandard2.0 lacks
public static int Last(int[] values) => values[^1];
}
public interface IClock
{
// Needs runtime support for default interface members
System.DateTime Now() => System.DateTime.UtcNow;
}
public interface IParse<TSelf> where TSelf : IParse<TSelf>
{
// Needs runtime support for static virtual members in interfaces
static abstract TSelf Parse(string text);
}go deeper
Know that some C# features work on .NET Framework once LangVersion is raised and some never do, because they need the newer runtime.
Sort features into compiler-only, library-dependent and runtime-dependent, with an example of each and the error id it produces on netstandard2.0.
Diagnose from the error id whether the fix is LangVersion, a polyfill or a target framework, and know that Roslyn checks RuntimeFeature fields in the core library.
Set the rule for shared old-target code: compiler-only and polyfillable features only, with runtime-dependent designs kept to projects that target modern .NET.
## The puzzle A team raises `LangVersion` to 12 on a `netstandard2.0` library. A `switch` expression with `when` guards compiles at once. A default method body in an interface fails with **CS8701**, *Target runtime doesn't support default interface implementation*, and a `static abstract` interface member fails with **CS8919**, *Target runtime doesn't support static abstract members in interfaces*. Same language version, different outcomes. The difference is what each feature needs at run time. ## Three kinds of features | Kind | Examples | What it needs | Fix on an old target | |---|---|---|---| | **Compiler-only** | switch expressions, recursive and relational patterns, file-scoped namespaces, nullable annotations | nothing beyond ordinary IL | raise `LangVersion` | | **Library-dependent** | `init` and records (`IsExternalInit`), `required` (`RequiredMemberAttribute`), index and range (`System.Index`, `System.Range`) | a type or member the compiler references | raise `LangVersion` and declare the type | | **Runtime-dependent** | default interface members, static abstract or virtual interface members, ref fields, inline arrays, covariant return types, ref struct generics | new behaviour in the type loader or method dispatch | a newer target framework; nothing else | A **compiler-only** feature is rewritten into constructs C# 1 could express: a switch expression becomes type tests and branches, a file-scoped namespace is the same metadata as a block-scoped one. Any runtime that ran C# 7.3 code runs it. A **library-dependent** feature compiles to code that names a type. `values[^1]` on an array needs a `System.Index` with a suitable constructor; on `netstandard2.0` it reports **CS0518** *Predefined type 'System.Index' is not defined or imported* and **CS0656** for its constructor. Declaring a compatible `internal struct Index` in the project makes it compile, because the compiler finds the type by name. A **runtime-dependent** feature produces metadata that an older runtime cannot load correctly. An interface method with a body is just a method in metadata, but only a runtime that knows how to dispatch to it can call it. .NET Framework's CLR does not. ## How the compiler knows Roslyn does not guess from the target framework name. It asks the core library, the assembly that defines `System.Object`, whether `System.Runtime.CompilerServices.RuntimeFeature` exists as a static class and carries a specific field: - `DefaultImplementationsOfInterfaces` for default interface members; - `VirtualStaticsInInterfaces` for static abstract and static virtual members; - `ByRefFields` for ref fields; - `CovariantReturnsOfClasses` for covariant returns; - `ByRefLikeGenerics` for `allows ref struct`. The check is `AssemblySymbol.RuntimeSupportsFeature` in the C# compiler. `netstandard2.0` and .NET Framework reference assemblies do not have these fields, so the answer is no. Because the lookup goes to the core library, declaring your own `RuntimeFeature` class in the project changes nothing: the build still reports CS8701. ## Why there is no workaround If the compiler let such code through, the library would load on .NET Framework and then fail when the runtime met an interface method body it cannot dispatch, or a static virtual call it does not understand. The compile-time error is the safe outcome. The difference from the library-dependent group is where the missing piece lives: a type can be compiled into your own assembly, while dispatch and type-loading rules live in the runtime that loads it. On `net10.0` every one of these fields is present, so the same interfaces compile with no extra setting. The docs list the minimum runtime for each: default interface members need .NET Core 3.0 or later, covariant returns .NET 5, static abstract members and ref fields .NET 7, inline arrays .NET 8, ref struct generics .NET 9. ## What to do instead 1. Keep the shared `netstandard2.0` surface to compiler-only and polyfillable features. 2. Replace a default interface method with an abstract base class or an extension method over the interface. 3. Replace static abstract members (generic math, `IParsable<TSelf>`-style factories) with an instance factory interface or a delegate. 4. Put code that needs these features in projects that target a modern .NET only, or multi-target the library (a .NET CLI matter) and compile the modern-only parts conditionally. ## Reading the errors The error ids tell you which group you hit: **CS8370**, **CS9058** and their siblings mean the language version is too low; **CS0518** or **CS0656** mean a type is missing; **CS8701**, **CS8919**, **CS9064**, **CS9171** and **CS8830** mean the target runtime cannot do it. One practical trap: the compiler reports declaration errors such as CS8701 before it binds method bodies, so an index-expression error can appear only after the interface errors are fixed.
- Can you declare your own RuntimeFeature class with DefaultImplementationsOfInterfaces to get past CS8701?No. The compiler looks for `RuntimeFeature` as a special type of the core library, the assembly that defines `System.Object`. A class declared in your project is not that type, so the check still fails and the build still reports CS8701. Only a target framework whose core library carries the field passes it.
- How do you tell from an error id whether raising LangVersion would help?The 'feature is not available in C# N' family, such as CS8370 for C# 7.3 or CS9058 for C# 11, means the language version is too low, so raising it helps. CS0518 and CS0656 mean a type is missing, which a polyfill may fix. CS8701, CS8919, CS9064, CS9171 and CS8830 mean the target runtime lacks support, and only a newer target framework fixes those.
Raising LangVersion is like teaching a writer a newer grammar for letters sent to an older reader. Sentences that only rearrange familiar words arrive fine; a sentence that uses a word the reader lacks can be rescued by enclosing a glossary; a sentence asking the reader to do something they cannot do is not fixed by any glossary.
- LangVersion latest enables every C# feature on any target
- Default interface members are just syntax the compiler rewrites
- Declaring RuntimeFeature yourself makes CS8701 go away
- The compiler decides runtime support from the target framework name
- Switch expressions need .NET Core because they are a C# 8 feature