Why would you use an immutable value in a dictionary?

安稳与你 提交于 2020-01-03 13:08:17

问题


The multiple answers to question "Multi value Dictionary" propose to use an immutable class as TValue in Dictionary<TKey, TValue> Class.

The accepted Jon Skeet's answer proposes a class Pair with readonly properties and @teedyay's answer to use the immutable Tuple.

What is the rationale (or the possible benefits) of such approaches?

And collateral question:
Why to make TFirst and TSecond readonly if the respective properties First and Second do not have setters anyway:

private readonly TFirst first;
private readonly TSecond second;

public TFirst First
{
   get { return first; }
}

public TSecond Second
{
   get { return second; }
}

Update:
I am using dictionaries with custom classes for values in them.
And the va lues are being updated.
What are the possible reasons (benefits) for me to make them immutable?

I see that Lookup<TKey, TElement> Class is also immutable and thought that I miss some benefits of using LINQ queries (?)
If so, can you give me examples what do I miss?


回答1:


Second question's answer is that the member variables are still settable without the readonly keyword - only within the class itself, but it is still possible.

BTW, this class seems like a solid candidate for a struct.




回答2:


Basically, immutability makes various things easier to read about in my experience. For example, one of the big pain points in Java's Calendar and Date classes is their mutability. It's all to easy to forget they're mutable, take a copy of a reference in the constructor, and then find that something else mutates the object you've got a reference to. So you start taking a defensive copy even if nothing is going to change the object... it all gets very annoying.

There's a time for mutability, of course - but in many cases immutability is simply nicer.



来源:https://stackoverflow.com/questions/15733406/why-would-you-use-an-immutable-value-in-a-dictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!