The multiple answers to question "Multi value Dictionary" propose to use an immutable class as TValue
in Dictionary<TKey, TValue>
Class.
对“多值字典”问题的多重回答建议在Dictionary
The accepted Jon Skeet's answer proposes a class Pair
with readonly properties and @teedyay's answer to use the immutable Tuple
.
公认的Jon Skeet的回答提出了一个具有readonly属性和@teedyay的类对来使用不可变元组。
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:
以及附带问题:如果各自的属性First和Second都没有setter,为什么要只进行TFirst和TSecond重新读取:
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?
我发现查找
2 个解决方案
#1
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.
第二个问题的答案是,成员变量仍然可以在没有readonly关键字的情况下进行设置——仅在类本身中,但仍然是可能的。
BTW, this class seems like a solid candidate for a struct
.
顺便说一句,这个类似乎是结构类的可靠候选者。
#2
4
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.
基本上,在我的经历中,不变性使各种事情更容易读懂。例如,Java的日历和日期类中最大的问题之一就是它们的可变性。很容易忘记它们是可变的,在构造函数中获取一个引用的副本,然后发现其他的东西会使你所引用的对象发生突变。所以你开始采取防御措施,即使什么都不会改变目标……这一切都很烦人。
There's a time for mutability, of course - but in many cases immutability is simply nicer.
当然,变性是有时间的——但在很多情况下,不变性只是更好而已。
#1
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.
第二个问题的答案是,成员变量仍然可以在没有readonly关键字的情况下进行设置——仅在类本身中,但仍然是可能的。
BTW, this class seems like a solid candidate for a struct
.
顺便说一句,这个类似乎是结构类的可靠候选者。
#2
4
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.
基本上,在我的经历中,不变性使各种事情更容易读懂。例如,Java的日历和日期类中最大的问题之一就是它们的可变性。很容易忘记它们是可变的,在构造函数中获取一个引用的副本,然后发现其他的东西会使你所引用的对象发生突变。所以你开始采取防御措施,即使什么都不会改变目标……这一切都很烦人。
There's a time for mutability, of course - but in many cases immutability is simply nicer.
当然,变性是有时间的——但在很多情况下,不变性只是更好而已。