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)
Before
Section titled “Before”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.