C# Feature - with expressions
Overview
Section titled βOverviewβWith expressions provide non-destructive mutation by copying an existing record or struct and applying targeted changes.
Introduced In
Section titled βIntroduced InβC# 9.0 (2020)
var updated = new Person(existing.Name, 37);var updated = existing with { Age = 37 };Gotchas & Best Practices
Section titled βGotchas & Best Practicesβ- With expressions shine most with immutable data models.
- Use them with records or value objects that should not be mutated in place.