A C# order-fulfilment service rewritten with a C# 12 primary constructor warns CS9124 on its feeCents parameter; what did the compiler generate, and what breaks?
answer
- parameters, not members
- used in a method means stored
- two copies of one value
- not readonly, so it can drift
basics
~20 sBecause feeCents is read in methods, the compiler captures it in a hidden, mutable field, while the FeeCents property initializer copies it into a second field. CS9124 warns about the two copies, which diverge once a method assigns the parameter.
solid answer
~50 sPrimary constructor parameters on a class (C# 12) are parameters in scope across the type body, not members: no property is generated, `this.feeCents` does not compile, and static members cannot use them (CS9105). A parameter used only in initializers or `base(...)` arguments is not stored; one used in a method or other instance member is **captured** into a hidden, non-`readonly` field. In the service, `FeeCents { get; } = feeCents` copies the value into the property's backing field, and `Fulfil` and `WaiveFee` use the captured copy. After `WaiveFee()` sets `feeCents = 0`, the property still reports 250. CS9124 flags exactly that double storage; CS9107 flags the same problem when the value also goes to a base constructor. Fix it by using one storage: read the property everywhere, or declare `private readonly int feeCents = feeCents;` so the field shadows the parameter and nothing is captured.
code
csharp · 21 linesvar service = new FulfilmentService(new Inventory(), feeCents: 250);
Console.WriteLine(service.Fulfil("ORD-9", quantity: 2));
service.WaiveFee();
Console.WriteLine($"FeeCents property: {service.FeeCents}, quoted fee: {service.QuoteFee()}");
class Inventory
{
public bool Reserve(string orderId, int quantity) => quantity > 0;
}
class FulfilmentService(Inventory inventory, int feeCents)
{
public int FeeCents { get; } = feeCents;
public string Fulfil(string orderId, int quantity) =>
inventory.Reserve(orderId, quantity) ? $"{orderId} reserved, fee {feeCents}" : $"{orderId} rejected";
public void WaiveFee() => feeCents = 0;
public int QuoteFee() => feeCents;
}go deeper
Recall that C# 12 lets a class declare constructor parameters in its header and that they are parameters, not properties.
Explain when the compiler stores a parameter in a hidden field and why that field is mutable.
Read CS9124 and CS9107 as double-storage bugs, trace which copy each member uses, and fix by choosing one storage per value.
Turn the capture rules into a team convention, such as explicit readonly fields, and decide whether these warnings fail the build.
## What a primary constructor is on a class Since **C# 12**, a non-record `class` or `struct` can declare parameters in its header: `class FulfilmentService(Inventory inventory, int feeCents)`. That list is a **primary constructor**. The documentation insists on reading its entries as **parameters**, not members: - They are in scope across the whole type body: field and property initializers, `base(...)` arguments in the header, methods, properties and local functions. - They are **not** members: `this.inventory` fails with CS1061, and a static member that uses one fails with `CS9105: Cannot use primary constructor parameter ... in this context`. - They become public properties only in `record` types; on a plain class nothing is generated for callers. - They can be **assigned** like any parameter. Every other constructor in the class must chain to the primary constructor with `: this(...)` (CS8862 otherwise), and the class gets no implicit parameterless constructor. ## Why anything is stored at all An ordinary constructor parameter lives only while the constructor runs. A method that runs later can read the value only if the compiler keeps a copy in the object. The design proposal describes the likely strategy as a private field with a mangled name, which is what the compiler emits; it does this per parameter, and only when it must. ## When the compiler stores a parameter The compiler decides storage from how each parameter is used: | Use of the parameter | Storage the compiler emits | |---|---| | Only in field or property initializers or `base(...)` arguments | None; it is an ordinary constructor parameter | | In a method, property accessor or other instance member | A hidden private field, named like `<feeCents>P` | | Not used at all | None, plus warning CS9113: Parameter is unread | Reflection on the example shows `<inventory>P` and `<feeCents>P` with `IsInitOnly` false, next to the property's `<FeeCents>k__BackingField`, which is `readonly`. A captured parameter is therefore **mutable state** that looks like a parameter at every use site. ## How the fulfilment service breaks The service uses `feeCents` twice: 1. `public int FeeCents { get; } = feeCents;` copies it into the property's backing field when the constructor runs. 2. `Fulfil`, `WaiveFee` and `QuoteFee` read or write `feeCents`, so the compiler also captures it. Now there are two fields holding the fee. `WaiveFee()` assigns `feeCents = 0`, which changes only the captured copy; `FeeCents` keeps returning 250. The SDK warns at build time: ```plaintext warning CS9124: Parameter 'int feeCents' is captured into the state of the enclosing type and its value is also used to initialize a field, property, or event. ``` A sibling warning covers inheritance. In `class AuditedService(Inventory inventory, string region) : ServiceBase(region)`, if `region` is also used in a method, the SDK reports `CS9107: Parameter 'string region' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.` The derived object then holds its own copy, which drifts from the base class's if either side changes. ## Fixes Choose **one** storage for each value: - **Read the member, not the parameter.** Use `FeeCents` inside the methods and remove `WaiveFee`'s write, or give the property a setter if the fee really changes. - **Declare an explicit field that shadows the parameter:** `private readonly int feeCents = feeCents;`. In initializers the name still means the parameter; everywhere else it means the field, so nothing is captured and the field is `readonly`; a class written that way builds with no CS9124 warning. - In a derived class, use the **base class's member** instead of the parameter you passed to `base(...)`. C# 14 has no `readonly` modifier for these parameters: writing `class S(readonly Inventory inventory)` fails to compile; the SDK reports `CS9190: 'readonly' modifier must be specified after 'ref'.` An explicit field is the way to get immutability. ## What to check in review - A CS9124 or CS9107 warning is almost always a real bug; treat it as one. - Look for assignments to primary constructor parameters in methods; the compiler does not warn about those. - Remember that validation must happen in an initializer (`= inventory ?? throw new ArgumentNullException(nameof(inventory))`), since there is no constructor body.
- Why does C# warn CS9107 when a captured primary constructor parameter is also passed to base(...)?The derived class stores its own copy of the value in a hidden field while the base class keeps whatever it stored from the constructor argument. The two are separate, so a change through the base class's member is invisible to the derived class's methods. Use the base member in the derived class instead.
- Can you make a C# primary constructor parameter readonly?Not in C# 14: `readonly` before the parameter does not compile, and a captured parameter is a mutable hidden field. Declare `private readonly T name = name;` instead; the field shadows the parameter outside initializers, so nothing is captured.
- Can a C# class with a primary constructor declare other constructors?Yes, but each must chain to the primary constructor with `: this(...)`, or the SDK reports CS8862. That guarantees the parameters are assigned on every path. The class also loses its implicit parameterless constructor.
- Primary constructor parameters on a class become public properties.
- Every primary constructor parameter is stored in a private field.
- A captured primary constructor parameter is stored in a readonly field.
- You can read a primary constructor parameter as this.feeCents.
- CS9124 is cosmetic; the property and the parameter always agree.
Assumes C# 14 on .NET 10; primary constructors on non-record classes arrived in C# 12, so code compiled with an older language version cannot use them.