As lead of a C# payments service moving to .NET 10, which C# 13 and 14 features would you adopt into team conventions, and would you ever ship with LangVersion preview?
answer
- adopt, restrict, leave to libraries
- encode in config, not a wiki
- preview means unsupported
- field moved between preview and release
basics
~20 sAdopt low-risk boilerplate removers such as System.Threading.Lock, the field keyword and collection expressions, and enforce them with analyzer settings; restrict risky ones like captured primary-constructor parameters; never ship LangVersion preview, which the compiler itself calls unsupported.
solid answer
~40 sI sort the new features into three groups. **Adopt by default**: `System.Threading.Lock` for new lock fields, `field` for setters that validate, collection expressions where the target type is spelled out, null-conditional assignment. **Adopt where it pays**: extension blocks when we want extension properties (converting classic extension methods is binary and source compatible), `params ReadOnlySpan<T>` on hot paths. **Library or generator code only**: `[OverloadResolutionPriority]`, `allows ref struct`, user-defined compound assignment, partial constructors and events. Primary-constructor parameters stay restricted for services, because they are captured and mutable, not readonly fields. The decisions live in `.editorconfig` rules (IDE0330, IDE0300, IDE0290 options), `EnforceCodeStyleInBuild`, and `WarningsAsErrors` for upgrade traps such as CS9258 and CS9216. Preview features stay in experiments: CS8652 calls them unsupported, and `field` changed between its C# 13 preview and C# 14.
code
ini · 13 linesroot = true
[*.cs]
# Adopt: System.Threading.Lock for lock fields (C# 13, .NET 9+)
csharp_prefer_system_threading_lock = true
dotnet_diagnostic.IDE0330.severity = warning
# Adopt: collection expressions only where the type is spelled on the left
dotnet_style_prefer_collection_expression = when_types_exactly_match
dotnet_diagnostic.IDE0300.severity = suggestion
# Restrict: no automatic conversion of service classes to primary constructors
csharp_style_prefer_primary_constructors = falsego deeper
Know that new C# features are optional, that a team may prefer some and forbid others, and that preview features are not for shipped code.
Explain how an .editorconfig rule such as IDE0330 or a collection-expression preference turns a team choice into something the tooling checks.
Justify each adoption with a concrete risk or benefit: Lock fallback warnings, the field rebinding trap, captured primary-constructor parameters, semantic-changing code fixes.
Own the policy: the buckets, how they are enforced in the build, how existing code migrates, and a firm rule that preview features never ship.
## The decision a lead owns A language upgrade hands a team about twenty new features at once. None of them is mandatory, and the compiler accepts old and new styles side by side. Without a decision, a codebase ends up with three ways to declare a list and two ways to lock, chosen by whoever touched the file last. The lead's job is to decide, per feature, whether it becomes **house style**, stays **allowed with a reason**, or is **restricted**, and then to make the tooling enforce that decision so code review is not the only line of defence. ## A three-bucket sort | Feature | Release | Default stance | Reasoning | |---|---|---|---| | `System.Threading.Lock` for lock fields | C# 13 / .NET 9 | adopt | same `lock` statement, dedicated type; IDE0330 flags `object` locks | | `field` keyword in accessors | C# 14 | adopt for validating setters | removes hand-written backing fields | | Collection expressions | C# 12 | adopt where the type is explicit | uniform syntax; a loose code fix can change the runtime type | | Null-conditional assignment | C# 14 | adopt | replaces `if (x != null) x.P = v;` | | Extension blocks | C# 14 | adopt when properties or static members help | classic methods keep working | | `params ReadOnlySpan<T>` | C# 13 | adopt on hot paths | can avoid a heap array per call | | `[OverloadResolutionPriority]`, `allows ref struct`, compound assignment operators, partial constructors and events | C# 13–14 | library or generator code only | API-design and generator tools | | Primary-constructor parameters in services | C# 12 | restrict | captured, mutable, not readonly fields | The reasoning column is where judgment lives. **Extension blocks** are a safe adoption because the docs state classic and block forms emit the same IL, so converting is binary and source compatible. **Collection expressions** come with a trap in the tooling: under `dotnet_style_prefer_collection_expression = when_types_loosely_match`, the default, the code fix can turn `IEnumerable<int> x = new int[] { 1, 2, 3 };` into a collection expression that produces a compiler-synthesized type instead of an array, which an `is` check or a cast can observe. **`[OverloadResolutionPriority]`** is, in the docs' words, intended for library authors avoiding ambiguity when adding overloads, not for application code. ## Encoding the decision A wiki page of preferences decays; analyzer configuration does not. - Put language-rule options and severities in a shared **`.editorconfig`**: `csharp_prefer_system_threading_lock`, `dotnet_style_prefer_collection_expression`, `csharp_style_prefer_primary_constructors`. - Set **`EnforceCodeStyleInBuild`** so IDE rules such as IDE0330 report during `dotnet build`, not only in the editor. - Add upgrade traps to **`WarningsAsErrors`**: CS9258 (a member named `field` silently rebound) and CS9216 (a `Lock` converted to another type, so `lock` falls back to Monitor-based locking). - Leave `LangVersion` unset on `net10.0` projects so the language follows the target framework. - Apply conventions to **new and touched code**; schedule mechanical sweeps separately, one rule at a time, with tests. ## Preview features `LangVersion` `preview` enables whatever the installed compiler has merged for the next release. On the .NET SDK 10.0.401, a C# 15 feature such as labeled `break` compiles under `preview` and fails without it with **CS8652**: the feature "is currently in Preview and *unsupported*". A policy that holds up: 1. Preview features are allowed in spikes, samples and feedback branches, never in code that ships. 2. A feature becomes available to the team when it ships in a released C# version, not when it appears in a preview SDK. 3. History is the argument: `field` was a C# 13 preview on the .NET 9 SDK, and before it shipped in C# 14 the compiler added error CS9273 for locals named `field` inside accessors. Code written against the preview had to change. 4. Library-level previews (`[RequiresPreviewFeatures]`, `EnablePreviewFeatures`) follow the same rule; the mechanics belong to the language-version lesson. ## Trade-offs to weigh - **Consistency vs churn**: a repo-wide conversion produces a huge diff that hides real changes and can alter semantics. - **Readability vs familiarity**: `field` and extension blocks are unfamiliar to engineers coming from older codebases; a short conventions page with examples pays for itself. - **Shared libraries**: projects targeting `netstandard2.0` default to C# 7.3, so features adopted in `net10.0` services do not automatically apply there. - **Reversibility**: style rules can be relaxed later; preview code in production cannot be un-shipped. There is no single right list. A defensible answer names the buckets, the reason for each placement, and the mechanism that makes the decision stick.
- Would you convert the existing codebase to the new features in one sweep?Usually not. A repo-wide conversion buries real changes in a huge diff and can alter behaviour; the collection-expression code fix under `when_types_loosely_match` can replace an array with a compiler-synthesized type. New and touched code follows the conventions, and mechanical sweeps run separately, one rule per change, with tests.
- When is turning on `LangVersion` `preview` reasonable?In a throwaway branch or sample, to evaluate a coming feature and give feedback before it ships. The compiler's CS8652 message calls preview features unsupported, and they move: `field` gained error CS9273 for locals named `field` between its C# 13 preview and its C# 14 release. Shipped code waits for the release.
- Which recent features would you leave to library authors, and why?`[OverloadResolutionPriority]`, ref struct interfaces with `allows ref struct`, user-defined compound assignment, and partial constructors and events. They solve API-evolution, performance-critical generic code or source-generator problems. Application code rarely needs them, so the convention is 'allowed with a reason in review' rather than house style.
- Adopt every new feature at once so the codebase looks modern.
- LangVersion preview is fine in production once the tests pass.
- A wiki page of preferences is enough; build-enforced analyzer rules are overkill.
- Primary-constructor parameters are readonly fields, so they suit every service.
- Converting classic extension methods to extension blocks breaks existing callers.
Assumes C# 14 on the .NET 10 SDK 10.0.401; the IDE rules cited apply from C# 12 or 13 onward, and C# 15 features exist there only as previews.