with Expressions on Records
Create modified copies of immutable objects.
with Expressions on Records is a free C# Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The with Expression
A with expression creates a copy of a record with some properties changed, leaving the original untouched. It is the idiomatic way to "update" immutable records.
using System;
record Point(int X, int Y);
var a = new Point(1, 2);
var b = a with { Y = 9 };
Console.WriteLine(a + " | " + b); // Point { X = 1, Y = 2 } | Point { X = 1, Y = 9 }Nondestructive Mutation
The original record is never modified. with performs a shallow copy and applies the listed changes to the copy only.
using System;
record User(string Name, int Age);
var original = new User("Ada", 30);
var older = original with { Age = 31 };
Console.WriteLine(original.Age + " " + older.Age); // 30 31Changing Multiple Properties
List several assignments inside the braces to change more than one property at once.
using System;
record Config(string Env, int Retries, bool Verbose);
var baseCfg = new Config("dev", 3, false);
var prod = baseCfg with { Env = "prod", Verbose = true };
Console.WriteLine(prod.Env + " " + prod.Retries + " " + prod.Verbose); // prod 3 TrueCopying Unchanged Properties
Properties you do not mention are copied as-is from the source record.
using System;
record Product(string Name, decimal Price, int Stock);
var p = new Product("Book", 12m, 5);
var discounted = p with { Price = 10m };
Console.WriteLine(discounted.Name + " " + discounted.Stock); // Book 5with and Value Equality
Because records use value equality, a copy that changes nothing is equal to the original.
using System;
record Coord(int X, int Y);
var a = new Coord(1, 2);
var b = a with { };
Console.WriteLine(a == b); // True (value equal)
Console.WriteLine(ReferenceEquals(a, b)); // False (different objects)Shallow Copy Semantics
with copies references, not deep object graphs. A nested mutable object is shared between the original and the copy.
using System;
using System.Collections.Generic;
record Cart(List<string> Items);
var a = new Cart(new List<string> { "x" });
var b = a with { };
b.Items.Add("y"); // shared list!
Console.WriteLine(a.Items.Count); // 2with on record structs
Record structs support with too, producing a modified copy of the value type.
using System;
record struct Size(int W, int H);
var s = new Size(4, 4);
var wide = s with { W = 8 };
Console.WriteLine(wide.W + "x" + wide.H); // 8x4Chaining with Expressions
You can chain with expressions to apply a sequence of changes, each producing a new record.
using System;
record Point(int X, int Y, int Z);
var p = new Point(0, 0, 0)
with { X = 1 }
with { Y = 2 }
with { Z = 3 };
Console.WriteLine(p); // Point { X = 1, Y = 2, Z = 3 }Builder-Free Updates
with removes the need for builder classes: just copy and tweak the fields you want.
using System;
record Email(string To, string Subject, string Body);
var draft = new Email("a@b.com", "Hi", "");
var ready = draft with { Body = "Welcome!" };
Console.WriteLine(ready.Body); // Welcome!Custom Copy Logic
Records generate a protected copy constructor that with uses. You can declare your own to customize how copies are made.
using System;
record Audited(string Name) {
public int Version { get; init; } = 1;
protected Audited(Audited other) {
Name = other.Name;
Version = other.Version + 1; // bump on every copy
}
}
var a = new Audited("x");
var b = a with { };
Console.WriteLine(a.Version + " " + b.Version); // 1 2Putting It Together
Modeling state transitions with with keeps an audit-friendly, immutable history.
using System;
record Order(string Id, string Status);
var created = new Order("O1", "Created");
var paid = created with { Status = "Paid" };
var shipped = paid with { Status = "Shipped" };
Console.WriteLine(created.Status + " -> " + paid.Status + " -> " + shipped.Status);Quick Check
Test your understanding of with expressions.
Recap
You learned record with expressions.
a with { Prop = value }copies and changes selected properties.- The original is never mutated; it is nondestructive.
- The copy is shallow, so nested mutable objects are shared.
- It works on records and record structs and can be chained.
Frequently asked questions
Is the “with Expressions on Records” lesson free?
Yes — the full text of “with Expressions on Records” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “with Expressions on Records”?
Create modified copies of immutable objects. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “with Expressions on Records” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- init-Only Setters
- The required Modifier
- Immutable Object Patterns
- with Expressions on Records