How to Copy a Record with Changes Using a with Expression in C#
order with { Size = "Large", Slices = 12 } makes a copy of the record order with those two properties changed and every other property copied as it is. The original record doesn't change.
Microsoft's reference: A with expression creates a copy of its operand with the specified properties and fields modified. The records reference calls this nondestructive mutation: you get a changed copy, and the record you started from keeps its values.
Syntax
<Type> <copy> = <original> with { <Property1> = <value1>, <Property2> = <value2> };
| Part | What it is |
|---|---|
<original> | The record to copy |
with | Makes a copy of <original> |
{ <Property1> = <value1>, ... } | The properties to change in the copy, written like an object initializer. Properties you leave out keep the original's values |
<copy> | A new variable that holds the copy |
The reference on what can go before with: The left-hand operand of a with expression can be a record type. It can also be a structure type, a tuple, or an anonymous type.
The Example
A console app called WithExpression has a Pizza record with a size, a topping and a number of slices. It orders a medium cheese pizza, then makes a bigger copy of it.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n WithExpression
cd WithExpression
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/WithExpression/WithExpression.csproj:
Determining projects to restore...
Restored /Users/you/source/WithExpression/WithExpression.csproj (in 39 ms).
Restore succeeded.
dotnet new console -n WithExpression creates a folder named WithExpression with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or WithExpression.csproj in Visual Studio.
Step 2: Write the Code
Replace everything in Program.cs. First the pizza and its copy:
Pizza order = new("Medium", "Cheese", 8);
// A copy of order with two properties changed.
Pizza bigger = order with { Size = "Large", Slices = 12 };
new("Medium", "Cheese", 8)creates the first pizza.order with { Size = "Large", Slices = 12 }copiesorder, setsSizeandSlicesin the copy, and keepsToppingfrom the original. The copy goes into a new variable,bigger.
Then the two lines that print them, and the record:
Console.WriteLine($"Original: {order}");
Console.WriteLine($"Copy: {bigger}");
record Pizza(string Size, string Topping, int Slices);
A record prints its type name and every property, so each line shows a whole pizza. record Pizza(string Size, string Topping, int Slices); declares the record in one line, at the end of the file after the code that runs.
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
Original: Pizza { Size = Medium, Topping = Cheese, Slices = 8 }
Copy: Pizza { Size = Large, Topping = Cheese, Slices = 12 }

The original still has Medium and 8: the with expression didn't change it. The copy has the two new values, Large and 12, and its topping, Cheese, came from the original.
The Complete File

Pizza order = new("Medium", "Cheese", 8);
// A copy of order with two properties changed.
Pizza bigger = order with { Size = "Large", Slices = 12 };
Console.WriteLine($"Original: {order}");
Console.WriteLine($"Copy: {bigger}");
record Pizza(string Size, string Topping, int Slices);
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Setting a property of the record directly, as in order.Size = "Large"; | error CS8852: Init-only property or indexer 'Pizza.Size' can only be assigned in an object initializer, or on 'this' or 'base' in an instance constructor or an 'init' accessor. | Make a changed copy with with. The records reference: Positional properties are immutable in a record class and a readonly record struct. |
Using with on an ordinary class | error CS8858: The receiver type 'Pizza' is not a valid record type and is not a struct type. | Declare the type with record instead of class |
Changing a property declared with only a get accessor, such as public int Price { get; } = 10; | error CS0200: Property or indexer 'Pizza.Price' cannot be assigned to -- it is read only | Give it an init accessor. The reference: Explicitly declared properties must have an init or set accessor to be changed in a with expression. |
Expecting the copy to get its own list from a list property, such as List<string> Toppings | Adding Olives to the copy's Toppings list changed the original too: both printed Cheese, Olives | Give the copy a new list: order with { Toppings = [.. order.Toppings] }. The reference: The result of a with expression is a shallow copy. |
FAQ
How do I change the record in a variable or a list?
Assign the copy back. order = order with { Size = "Large" }; left order as Pizza { Size = Large, Topping = Cheese, Slices = 8 }. In a list, orders[1] = orders[1] with { Size = "Large", Slices = 12 }; replaced the second pizza with its changed copy, Pizza { Size = Large, Topping = Ham, Slices = 12 }.
Does with work on structs, tuples and anonymous types?
Yes. start with { X = 5 } turned a struct Point at 1,2 into one at 5,2, an anonymous object went from { Food = Soup, Price = 4 } to { Food = Soup, Price = 3 }, and a tuple went from (4, 3) to (10, 3).
Is a copy with no changes the same object?
No. order with { } made a copy that == called equal, True, because records compare their values, but ReferenceEquals(order, same) printed False: it's a second object.
Conclusion
Write original with { Property = value } to get a copy of a record with only those properties changed. The original keeps its values, so put the copy in a variable, or back in the same one when you want to replace the record.
Additional Resources
- The with expression — the expression, what it can copy and how the copy is made.
- Records: nondestructive mutation —
withon records, init accessors and shallow copies.
