Immutability: record class vs record struct
#9680
-
|
I wonder why I suppose when records were introduced they were supposed to be immutable types. And there were no thoughts that mutability will be needed later so I'm asking because from my point of view |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Very intentional. The issue with mutability in reference types is unintentional mutation far from where you are creating an using the record, which can cause many fun issues (such as losing a key in a dictionary). Value types are copied by value, not by reference, so any mutations you make only affect your copy of the data, not anyone else's. Passing a value type by |
Beta Was this translation helpful? Give feedback.
-
|
You can make a |
Beta Was this translation helpful? Give feedback.
Very intentional. The issue with mutability in reference types is unintentional mutation far from where you are creating an using the record, which can cause many fun issues (such as losing a key in a dictionary). Value types are copied by value, not by reference, so any mutations you make only affect your copy of the data, not anyone else's. Passing a value type by
refis a conscious and visible choice in the code, so mutations from that are obvious and obviously intended.