The true definition of immutability?

后端 未结 10 1907
被撕碎了的回忆
被撕碎了的回忆 2021-02-09 03:15

I am wondering how immutability is defined? If the values aren\'t exposed as public, so can\'t be modified, then it\'s enough?

Can the values be modified inside the type

10条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-09 03:50

    The previous posters have already stated that you should assign values to your fields in the constructor and then keep your hands off them. But that is sometimes easier said than done. Let's say that your immutable object exposes a property of the type List. Is that list allowed to change? And if not, how will you control it?

    Eric Lippert has written a series of posts in his blog about immutability in C# that you might find interesting: you find the first part here.

提交回复
热议问题