C# Academy · Lesson

with Expressions on Records

Create modified copies of immutable objects.

Lesson 4 of 413 steps

with Expressions on Records is a free C# Academy lesson on CoddyKit. This is 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, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 4 lessons in total.

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 31

Changing 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 True

Copying 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 5

with 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); // 2

with 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); // 8x4

Chaining 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 2

Putting 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.
Free to start

Learn C# with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
93
Lessons
346

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. 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. The C# Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

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

  1. init-Only Setters
  2. The required Modifier
  3. Immutable Object Patterns
  4. with Expressions on Records
← Back to C# Academy