According to MSDN,
The only argument I can see for making anything immutable is to help give reference types "value type" like behavior, such as the System.String class. Making value types immutable, though, actually seems to take away some value type behavior (you can change the width of a Rect, but not the month of a DateTime?).
Note the item highlighted in red. My question is, why should a struct be immutable? There are cases where it would make sense, but why should this be a general rule? It doesnt seem appropriate in so many cases. Look at the DotNet classes such as Rectangle and Point. Why does Microsoft recommend that a struct be immutable? Mutability is important when manipulating objects. Consider the amount of garbage that is generated when manipulating strings. Even with value types, where there is no garbage, it makes a lot more work for the CPU when every little tweak generates a new instance and can make code messy, especially if operator overloads arent used.MSDN said:Struct Usage Guidelines
It is recommended that you use a struct for types that meet any of the following criteria:
- Act like primitive types.
- Have an instance size under 16 bytes.
- Are immutable.
- Value semantics are desirable.
The only argument I can see for making anything immutable is to help give reference types "value type" like behavior, such as the System.String class. Making value types immutable, though, actually seems to take away some value type behavior (you can change the width of a Rect, but not the month of a DateTime?).