Why can mutable structs cause subtle bugs?
Short answer
Because structs copy on assignment and when stored in some collections/properties, mutating a copy does not change the original — leading to lost updates. Prefer readonly structs.
Advertisement
AModel answer
Because structs copy on assignment and when stored in some collections/properties, mutating a copy does not change the original — leading to lost updates. Prefer readonly structs.
struct P { public int X; }
var list = new List<P>{ new P() };
list[0].X = 5; // compile error / would mutate a copyReal-world example
A mutable struct field on a class property silently discards writes.