Unexpected Inconsistency in C# Records: A `with` Operator Gotcha

2025-07-22
Unexpected Inconsistency in C# Records: A `with` Operator Gotcha

The author discovered an unexpected inconsistency when using C# records. When updating records containing derived data with the `with` operator, the derived data isn't recalculated, leading to inconsistencies. This stems from the `with` operator not calling the constructor but instead using a copy constructor to create a copy and then modify properties. Several solutions are proposed, including avoiding `with` on complex records, writing a Roslyn analyzer to detect the issue, using `Lazy` for deferred property computation, and requesting a language change. This post highlights a potential pitfall in C# records, cautioning developers about using the `with` operator, especially with derived data.

Read more
Development Records with operator