Skip to content

with expressions

Context: with expressions create a new record instance by copying an existing one and modifying specified properties. This enables non‑destructive mutation.

public record Person(string FirstName, string LastName, int Age);
var original = new Person("Alice", "Smith", 30);
var updated = original with { Age = 31 };
Console.WriteLine(original); // Person { FirstName = Alice, LastName = Smith, Age = 30 }
Console.WriteLine(updated); // Person { FirstName = Alice, LastName = Smith, Age = 31 }
public record Address(string City, string Street);
public record Person(string Name, Address Address);
var p1 = new Person("John", new Address("Paris", "Rue A"));
var p2 = p1 with { Address = p1.Address with { Street = "Rue B" } };

Configuration objects: When you have an immutable configuration record, you can create a modified version for a specific environment using with.

Example: In ASP.NET Core Options pattern, you could use records and with to derive test configurations.