In C#, a method takes a TenantSettings record and returns settings with { TimeoutSeconds = 60 }; passed an EnterpriseSettings derived record, what does it return, and what can it change?
answer
- virtual dispatch on the copy
- run-time type survives
- compile-time type limits the braces
- pattern match to reach derived members
basics
~20 sIt returns an EnterpriseSettings with every derived member, such as Region, copied and TimeoutSeconds set to 60, because the hidden clone method is virtual. The braces may only name members of the compile-time type TenantSettings.
solid answer
~40 sThe record's clone method is **virtual** and each derived record overrides it, so `settings with { ... }` copies the **run-time type**: passed an `EnterpriseSettings`, the method returns an `EnterpriseSettings` whose `Region` and other derived members are copied, with only `TimeoutSeconds` changed. Nothing is sliced off. But the expression is **typed** as the receiver's compile-time type, `TenantSettings`, and the brace list is checked against that type, so `settings with { Region = "us-east" }` fails with CS0117, "'TenantSettings' does not contain a definition for 'Region'". To change a derived member, narrow first: `settings is EnterpriseSettings e ? e with { Region = ... } : settings`. A generic `T Bump<T>(T s) where T : TenantSettings` can use `with` and return `T`.
code
csharp · 11 linesTenantSettings original = new EnterpriseSettings("alpha", 30, "eu-west");
TenantSettings copy = original with { TimeoutSeconds = 60 };
Console.WriteLine(copy.GetType().Name);
Console.WriteLine(copy);
Console.WriteLine(((EnterpriseSettings)copy).Region);
record TenantSettings(string TenantId, int TimeoutSeconds);
record EnterpriseSettings(string TenantId, int TimeoutSeconds, string Region)
: TenantSettings(TenantId, TimeoutSeconds);go deeper
Recall that a with copy is always the same kind of record as the object you started from, even through a base-typed variable.
Explain the virtual clone method that each derived record overrides, and why the braces are checked against the compile-time type.
Review helpers written against a base record: no slicing to compensate for, CS0117 for derived members, and pattern matching or a constrained generic to reach them.
Judge whether shared helpers should take base records or generic constrained types, so callers keep their static type without casts.
## The scenario A settings service has a record hierarchy: ```csharp record TenantSettings(string TenantId, int TimeoutSeconds); record EnterpriseSettings(string TenantId, int TimeoutSeconds, string Region) : TenantSettings(TenantId, TimeoutSeconds); ``` A helper written against the base type raises timeouts: ```csharp static TenantSettings Relax(TenantSettings settings) => settings with { TimeoutSeconds = 60 }; ``` Two questions come up in review: does an enterprise tenant lose its `Region` when it goes through `Relax`, and can the helper change `Region` too? ## The copy keeps the run-time type For a record class the compiler synthesizes a public **clone method** (named `<Clone>$` in metadata). In an unsealed record that derives from `object` it is **virtual** (abstract in an abstract record); in each derived record the synthesized clone **overrides** the base one and calls the derived copy constructor. A `with` expression calls this method virtually, so: 1. `Relax(new EnterpriseSettings("alpha", 30, "eu-west"))` calls `EnterpriseSettings`'s clone; 2. the derived copy constructor chains to the base copy constructor and then copies the derived fields, so `TenantId`, `TimeoutSeconds` and `Region` are all copied; 3. `TimeoutSeconds = 60` is assigned on that copy. The result prints `EnterpriseSettings { TenantId = alpha, TimeoutSeconds = 60, Region = eu-west }`. Microsoft's documentation states it as a rule: the result of a `with` expression has the same run-time type as the operand. There is **no slicing** — a copy through a base-typed variable never quietly turns an `EnterpriseSettings` into a plain `TenantSettings`. If the base record is `abstract`, its clone method is abstract and every concrete derived record supplies one, so `with` through the abstract base type still works. ## The braces are checked against the compile-time type The record specification says the clone's result is **converted to the receiver's type**, and the brace list names members of that type. Inside `Relax`, the receiver's type is `TenantSettings`, so: - `settings with { TimeoutSeconds = 60 }` compiles — `TimeoutSeconds` is declared on `TenantSettings`; - `settings with { Region = "us-east" }` does not: ```plaintext error CS0117: 'TenantSettings' does not contain a definition for 'Region' ``` - the expression's static type is `TenantSettings`, even though the object is an `EnterpriseSettings`. ## Reaching a derived member | Need | Code | |---|---| | change a base member, keep everything else | `settings with { TimeoutSeconds = 60 }` | | change a derived member when present | `settings is EnterpriseSettings e ? e with { Region = "us-east" } : settings` | | keep the caller's static type | `static T Relax<T>(T s) where T : TenantSettings => s with { TimeoutSeconds = 60 };` | The generic form compiles on C# 14: a `with` on a type parameter constrained to a record type has type `T`, so a caller passing `EnterpriseSettings` gets `EnterpriseSettings` back without a cast. ## Where it goes wrong in production - **Assuming slicing and compensating for it.** Code that re-adds derived members after a base-typed `with` ("because the copy lost them") is fighting a problem that does not exist. - **Casting the result blindly.** `(EnterpriseSettings)Relax(x)` is safe only when `x` was one; the copy is whatever `x` was. - **A hand-written copy constructor in the base** applies to every derived `with` too, because each derived copy constructor chains to it — useful for cloning a shared list, surprising if it was meant for the base alone. - **The clone method is not yours to change.** You cannot declare, call or override it; to change what a copy contains, declare a copy constructor. Equality between the original and the copy, and how `EqualityContract` treats derived records, is covered with record classes.
- In C#, can a generic method T Relax<T>(T s) where T : TenantSettings return s with { TimeoutSeconds = 60 } as T?Yes, on C# 14 it compiles and the expression has type `T`, so a caller passing an `EnterpriseSettings` gets an `EnterpriseSettings` back with no cast. The brace list is still limited to members of the constraint type, `TenantSettings`.
- In C#, does with through an abstract base record type still work?Yes. In an abstract record the synthesized clone method is abstract, and every concrete derived record overrides it, so `baseVariable with { ... }` dispatches to the concrete type's clone and returns that concrete type.
- In C#, if the base record declares its own copy constructor, which with expressions use it?All of them, including copies of derived records: each derived record's synthesized copy constructor first calls the base copy constructor, so the base's custom copying — cloning a list, say — also runs when a derived instance is copied through any variable.
- A with through a base-typed variable slices the copy down to the base record.
- The braces may name any member of the run-time type.
- The result of the with expression is statically typed as the derived record.
- You can override the clone method to change what with copies.
- with through an abstract base record does not compile.