C# Reference ยท Operators and Expressions
C# with expressions
Reference for the C# with expression: copy a record, struct or anonymous type and change some properties without touching the original value.
A with expression makes a copy of an existing value and lets you change some of its members in the same step. The original is never modified. This is called non-destructive mutation, and it is the normal way to "edit" immutable data in C#.
with works on records, record structs, ordinary structs, tuples, and anonymous types. The members you assign must be settable through an init or set accessor, or be a field.
var moved = point with { X = 10 };
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
newValue = source with { Member1 = expr1, Member2 = expr2 };
| Name | Description |
|---|---|
source |
An expression of a record, struct, tuple or anonymous type. |
Member = expr |
An assignment to an accessible instance property (with set or init) or field. The list may be empty. |
Returns: a new value of the same compile-time type as source.
Errors: assigning to a member that has only a getter, or that does not exist, is a compile error. If source is a null reference to a record, the expression throws NullReferenceException.
Usage notes
- For a record class, the compiler calls a hidden clone method, then runs your assignments on the copy. Because of this,
withalso works when the static type is a base record and the object is a derived one: the copy keeps the derived type. - The copy is shallow. Reference-type members (lists, arrays, other classes) are shared between the original and the copy, as the second example shows. Replace them with new objects inside the
withblock when you need independence. - For a struct the value is copied, then the assignments are applied. Before C# 10 this only worked for anonymous types and records; now any struct (and any tuple) can be used.
- Properties declared with
initcan be assigned in awithblock, even though they cannot be set on an existing object afterwards. - Positional record properties are
init-only by default, sowithis the intended way to change them. - You can assign several members in one expression, and later assignments may refer to the original through the source variable, not through the new one.
- An empty
with { }makes a plain copy. This is the easiest way to clone a record. - A
withon a class that is not a record does not compile: only types with a copy mechanism the compiler knows about are accepted. - Equality checks on records compare values, so
original == reissueisfalseafter a change, whileoriginal == original with { }istrue.