Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should structs always be immutable?

Tags:

c#

I was reading a couple of threads on here about structs and there is/was one about structs and how they should be representing immutable values (eg like a digit - 1) because of their value type behaviour/semantics.

But on the other hand, structs represent things like phone numbers, which can change for the same household.

Is this a hard and fast rule?

like image 676
GurdeepS Avatar asked Dec 02 '25 01:12

GurdeepS


1 Answers

A phone number does not change; you just get a different one and discard the old one. The old one is still the same it always was. Same with dates, numbers, etc. - think of this when approaching structs. They are a way to encapsulate a value - which simply is; not the usage of the value, which changes.

like image 86
Rex M Avatar answered Dec 03 '25 18:12

Rex M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!