Code inspection: Captured primary constructor parameter is mutable

Last modified: 07 August 2024

Primary constructors for non-record types make your type definitions more compact. However, as a downside, they introduce a potential problem of "capturing" primary constructor parameters inside instance member bodies. All parameter variables in C# are mutable, which means that any instance member can change its value after the initialization, and there is no syntax to enforce the parameter to be readonly.

This inspection reports cases when "captured" primary constructor parameters are actually modified by the members.

A quick-fix helps explicitly express the mutable class-level state by introducing an ordinary mutable field.