A netstandard2.0 C# library shared by net48 and net10.0 apps needs records and required members; what fails, and how is it fixed?
answer
- two layers of errors
- C# 7.3 first, then missing types
- CS0518 and CS0656
- internal types in System.Runtime.CompilerServices
basics
~10 sAt 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.
solid answer
~40 s`netstandard2.0` compiles as C# 7.3, so a `required` property is rejected with CS8370, 'feature is not available in C# 7.3', and the 7.3 parser does not even recognise `record`. Setting `LangVersion` to 12 gets past the language check, and the errors change: `init` accessors (which records generate) report CS0518, predefined type `System.Runtime.CompilerServices.IsExternalInit` is not defined or imported, and `required` reports CS0656 for missing `RequiredMemberAttribute` and `CompilerFeatureRequiredAttribute` constructors. These are marker types the compiler writes into metadata; `netstandard2.0` does not contain them. The compiler matches them by full name, so declaring them in the library as `internal` classes fixes the build, and `SetsRequiredMembersAttribute` is added when a constructor sets every required member. Consumers keep the rules: even a C# 7.3 consumer built by the current compiler must set required members.
code
csharp · 22 lines// Polyfills.cs in the netstandard2.0 library (LangVersion 12)
namespace System.Runtime.CompilerServices
{
internal static class IsExternalInit { }
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
internal sealed class RequiredMemberAttribute : Attribute { }
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = false)]
internal sealed class CompilerFeatureRequiredAttribute : Attribute
{
public CompilerFeatureRequiredAttribute(string featureName) => FeatureName = featureName;
public string FeatureName { get; }
public bool IsOptional { get; init; }
}
}
namespace System.Diagnostics.CodeAnalysis
{
[AttributeUsage(AttributeTargets.Constructor, AllowMultiple = false, Inherited = false)]
internal sealed class SetsRequiredMembersAttribute : Attribute { }
}go deeper
Remember that netstandard2.0 compiles as C# 7.3, so records and required members need a higher LangVersion before anything else.
Explain the second wave of errors: CS0518 and CS0656 mean a marker type is missing, and internal declarations with the right namespace and shape fix them.
Know which features a polyfill can rescue (missing types) and which it cannot (runtime support), and check what consumers on older compilers and language versions will experience.
Weigh whether the shared library should carry polyfills at all, given the unsupported language-version setting, versus moving shared DTOs to code that only the net10.0 side compiles.
## The setup A common repository shape: a `net48` Windows service, a `net10.0` API, and a `netstandard2.0` library both of them reference. A developer wants to model DTOs in the shared library as `record` types and mark mandatory properties `required`. The library's default language version is the problem, and fixing it takes two steps. ## Step one: the language version The .NET SDK gives `netstandard2.0` the default **C# 7.3**. Records arrived in C# 9 and required members in C# 11, so the compiler rejects them with **CS8370**: *Feature 'required members' is not available in C# 7.3. Please use language version 11.0 or greater.* (For `record` the 7.3 parser does not even recognise the keyword and reports a cascade of unrelated errors such as CS0246.) Setting `<LangVersion>12</LangVersion>` in the library tells the compiler to accept C# 12 syntax. That alone does not finish the job. ## Step two: the missing types With C# 12 enabled, the same code fails differently: - **CS0518** *Predefined type 'System.Runtime.CompilerServices.IsExternalInit' is not defined or imported* on every `init` accessor, including the ones a positional record generates. - **CS0656** *Missing compiler required member 'System.Runtime.CompilerServices.RequiredMemberAttribute..ctor'* and the same for `CompilerFeatureRequiredAttribute..ctor` on a type with `required` members. These features are implemented by the compiler, but the compiler records them in metadata using types it expects the core library to provide: | Feature | Type the compiler needs | What it is used for | |---|---|---| | `init` accessors, records | `IsExternalInit` | a `modreq` on the setter so older compilers cannot call it as a normal setter | | `required` members | `RequiredMemberAttribute` | marks the type and each required member | | `required` members | `CompilerFeatureRequiredAttribute` | placed on constructors, next to an `[Obsolete]` error, so compilers that do not understand required members refuse to call them | | constructors that set everything | `SetsRequiredMembersAttribute` | tells the compiler a constructor satisfies all required members | Newer frameworks ship these types publicly in the core library. `netstandard2.0` and .NET Framework do not. ## The fix: polyfills The compiler looks these types up by **namespace and name**, not by assembly, so the library can declare its own copies: 1. Add one file with `IsExternalInit`, `RequiredMemberAttribute` and `CompilerFeatureRequiredAttribute` in `System.Runtime.CompilerServices`, and `SetsRequiredMembersAttribute` in `System.Diagnostics.CodeAnalysis`. 2. Declare them **`internal`**, so they do not become part of the library's public API. 3. Copy the shape of the real types (attribute targets, constructor, `FeatureName`, `IsOptional`), so the compiler finds the members it calls. The .NET runtime uses the same pattern for its own multi-targeted libraries: CoreLib's `IsExternalInit.cs` is `public` only when compiled into CoreLib and `internal` otherwise, and `System.Text.Json` compiles that file into its builds for targets that are not .NET Core. ## What the compiler writes into the library Reading the built `netstandard2.0` assembly with reflection shows why the types must exist at compile time: - the `Name` setter carries a required custom modifier of type `System.Runtime.CompilerServices.IsExternalInit`, so a compiler that does not know `init` cannot treat it as an ordinary setter; - the `Customer` constructor carries `[CompilerFeatureRequired("RequiredMembers")]` and `[Obsolete("Constructors of types with required members are not supported in this version of your compiler.", true)]`, so an old compiler fails instead of silently ignoring the required members. The compiler emits references to these types, so it needs to find them. ## What consumers see The metadata travels with the library, so consumers keep the rules: - A consumer compiled by the current compiler must set required members, even at C# 7.3: `new Customer()` fails with **CS9035**. - Assigning an `init` property after construction fails with **CS8852**. - The net10.0 API simply uses the records; its own core library already has the real types, and the library's internal copies are invisible to it. ## What this does not unlock Polyfills help only when the missing piece is a type. Features that need the runtime itself, such as default interface members or static abstract interface members, still fail on `netstandard2.0` whatever `LangVersion` says, and attributes used for nullable analysis such as `[NotNullWhen]` are also absent from `netstandard2.0` (CS0234) and need their own internal copies. Also keep in mind that a language version above the framework's default is officially unsupported, which is a decision for the team, not just a build fix.
- Why should the polyfill types be internal rather than public?They exist only so the compiler can find them while compiling this library. Making them `internal` keeps them out of the library's public API, so consumers never see or depend on a `System.Runtime.CompilerServices` type defined in your assembly. The runtime's own shared source does the same: `IsExternalInit` is `public` only inside CoreLib.
- The net48 service consumes the library and is still compiled as C# 7.3. Can it skip a required member?Not with the current compiler. The required-member metadata is in the library, and the compiler enforces it on every consumer: `new Customer()` without `Name` fails with CS9035 even at C# 7.3, and assigning an `init` property later fails with CS8852. The consumer can construct records through their constructors, but it cannot use C# 9 syntax such as `with` until its own language version allows it.
- Does a polyfill let you use default interface members on netstandard2.0?No. Default interface members need runtime support, which the compiler checks by looking for `RuntimeFeature.DefaultImplementationsOfInterfaces` in the core library. Declaring your own `RuntimeFeature` class does not satisfy that check; the build still fails with CS8701.
- Raising LangVersion alone makes records compile on netstandard2.0
- You must reference a newer framework assembly to get IsExternalInit
- Polyfill types should be public so consumers can use them
- A C# 7.3 consumer can ignore required members from the library
- Polyfills can unlock any C# feature on an old target