Should structures always be immutable?

I read a couple of threads here about structures and there was / was one about structures and how they should represent immutable values ​​(for example, as a digit - 1) due to their behavior / semantics like value.

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

Is this a tough and quick rule?

+3
source share
4 answers

The phone number does not change; you just get another and discard the old one. The old one is the same as always. The same goes for dates, numbers, etc. - think about it when approaching structures. This is a way to encapsulate a value that simply is; rather than using a value that changes.

+5
source

Yes, structures should always be unchanged! Variable structures can cause terrible headaches, as their use can create very strange behavior.

+3
source

, . , : , . 555-555-1234 - 555-555-1234, - 555-555-5678.

, .NET Framework. , WPF Point Size . , , something.Location.X = newX.

+1

!=

, , , , . int x = 5; x++; . 5++; .

, (, myStruct.MyInt++), , MyInt. , , , .

? , myStruct.MyInt , , , , .

0

Source: https://habr.com/ru/post/1733724/


All Articles