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?
answer
- officially unsupported, widely practised
- which features are safe to allow
- pinned number, scoped to old targets
- who owns the polyfills
- exit plan: move targets
basics
~20 sPossibly, 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.
solid answer
~50 sI would treat it as a governed exception, not a default. The docs state that a C# version newer than the target's default isn't supported, so nobody stands behind compiler output for that pairing, even though the compiler blocks the features that cannot work (CS8701, CS8919). The payoff is real: nullable reference types, pattern matching, file-scoped namespaces, and records or required members with internal polyfills, all in code both the net48 service and the net10.0 API share. My rules would be: set a pinned number (say 12) only for the net48 and netstandard2.0 projects, in Directory.Build.targets; one internal polyfill file owned by the platform team; an allow-list limited to compiler-only and polyfillable features; tests that run on the real .NET Framework runtime; and a dated plan to shrink the netstandard2.0 surface as services move to net10.0.
code
xml · 10 lines<Project>
<!-- Directory.Build.targets: a single-target project sets TargetFramework
after Directory.Build.props is imported, so the condition lives here -->
<PropertyGroup Condition="'$(TargetFramework)' == 'net48' Or '$(TargetFramework)' == 'netstandard2.0'">
<LangVersion>12</LangVersion>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net48' Or '$(TargetFramework)' == 'netstandard2.0'">
<Compile Include="$(MSBuildThisFileDirectory)build/Polyfills.cs" Link="Polyfills.cs" />
</ItemGroup>
</Project>go deeper
Know that .NET Framework and netstandard2.0 default to C# 7.3 and that going higher is possible but officially unsupported.
Explain which features work after raising LangVersion and which polyfills the shared library would need for records, required members and nullable attributes.
Set it up safely: a pinned number scoped through Directory.Build.targets, internal polyfills, and tests that run on the real .NET Framework runtime.
Own the trade-off: an unsupported configuration and polyfill upkeep against one language across the repository, with a dated plan to move workloads to net10.0.
## The decision A repository holds a `net48` Windows service, a `net10.0` API and a `netstandard2.0` library both reference. The service and the library compile as **C# 7.3**, the SDK default for those targets. Developers working mostly in C# 14 on the API want the same language in the shared code. The lead must decide whether to raise `LangVersion` on the old targets, and if so, how far and under what controls. There is no single right answer; there is a set of trade-offs to own explicitly. ## What the platform says The C# docs are explicit: *using a C# language version newer than the version associated with your target TFM isn't supported*. Visual Studio disables the language-version picker for the same reason. Unsupported does not mean broken. It means: - nobody guarantees that the compiled output behaves correctly on that runtime; - a problem caused by the combination is yours to diagnose; - each new language feature may depend on types or runtime behaviour the old target lacks. The compiler gives a useful safety net: features that need runtime support fail at compile time (CS8701 for default interface members, CS8919 for static abstract members, CS9064 for ref fields), and features that need missing types fail with CS0518 or CS0656 until someone declares them. The .NET runtime itself multi-targets libraries such as `System.Text.Json` to `netstandard2.0` and .NET Framework and compiles an internal `IsExternalInit` into those builds, which shows the pattern works when someone owns it. ## The case for raising it - **Nullable reference types** in shared contracts, which pay off most in code that two services depend on. - **Modern patterns** and switch expressions, which are pure compiler features. - **Records and required members** for DTOs, with internal polyfills. - One C# style across the repository, so reviewers and tools do not keep two dialects in mind. ## The case against - An unsupported configuration in code that ships to production. - **Polyfills to own**: `IsExternalInit`, `RequiredMemberAttribute`, `CompilerFeatureRequiredAttribute`, `SetsRequiredMembersAttribute`, nullable analysis attributes such as `[NotNullWhen]`, and more with every feature someone adopts. - Confusion when code compiles in the library but a runtime-dependent design cannot be moved there. - Effort spent making old targets comfortable instead of moving workloads to `net10.0`. ## If the answer is yes: the rules 1. **Pin a number**, such as `12`, never `latest`, `latestMajor` or `default`, so an SDK upgrade cannot change the language by itself. 2. **Scope it** to the old targets in `Directory.Build.targets`, not `Directory.Build.props`: a single-target project sets `TargetFramework` after `Directory.Build.props` is imported, so a condition on it there never matches. 3. **Allow-list features**: compiler-only features and the polyfilled ones. Reject designs that need runtime support in shared code; the compiler enforces part of this, review enforces the rest. 4. **One polyfill file**, `internal`, linked into the old-target projects and owned by a named team. 5. **Test on the real runtime**: run the `net48` service's tests on .NET Framework, not only the `net10.0` side. 6. **Write down the exit**: which code moves to `net10.0`, so the `netstandard2.0` surface shrinks instead of growing. ## If the answer is no Keeping C# 7.3 is a defensible choice when the shared library is small, stable or near retirement. Then the effort goes into moving the Windows service to `net10.0` (the generic host supports Windows services) and dropping `netstandard2.0` once nothing needs it, after which the API's C# 14 default applies everywhere. ## Signals that the exception is failing - The polyfill file grows every release as developers adopt more features. - Reviews keep rejecting runtime-dependent designs aimed at the shared library. - A production defect is traced to the unsupported pairing rather than to ordinary code. ## How I would frame it to the team | Option | Benefit | Cost | |---|---|---| | Stay on C# 7.3 | supported, nothing to own | two dialects, no nullable contracts in shared code | | Raise to a pinned number with polyfills | modern syntax and nullable contracts in shared code | unsupported pairing, polyfills and rules to maintain | | Move targets to `net10.0` | supported modern C#, runtime features too | migration work for the Windows service | The usual outcome is the middle option as a bridge, with the third as the destination and a date on it.
- Why put the condition in Directory.Build.targets rather than Directory.Build.props?The SDK imports `Directory.Build.props` before the project body, so in a single-target project `$(TargetFramework)` is still empty there and the condition never matches; `LangVersion` stays at the 7.3 default. `Directory.Build.targets` is imported after the project body, when `TargetFramework` is set.
- What would make you reverse the decision later?Signs that the exception is growing instead of shrinking: more polyfills added per release, developers pushing runtime-dependent designs into the shared library, or a defect traced to the unsupported pairing. Equally, once the Windows service runs on net10.0 and nothing else needs netstandard2.0, the library can target net10.0 and the exception disappears.
- Microsoft supports any LangVersion the installed compiler accepts
- Set LangVersion latest so old projects keep up automatically
- Polyfills make every C# feature safe on .NET Framework
- A TargetFramework condition in Directory.Build.props scopes LangVersion
- Raising LangVersion needs no testing on the .NET Framework runtime