的多个答案质疑多值字典建议使用一个不变的类 TValue
在 字典< TKEY的,TValue>
类。
The multiple answers to question "Multi value Dictionary" propose to use an immutable class as TValue
in Dictionary<TKey, TValue>
Class.
在接受乔恩斯基特的回答提出了一种类对
与只读属性和 @ teedyay的回答使用一成不变的 元组
。
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?
和抵押的问题: 为什么使TFirst和TSecond只读如果各自的属性第一和第二没有setter方法呢:
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; }
}
更新: 我使用的字典,可以自定义类在他们的价值观。 而VA梅毒正在更新。照片 可能是什么原因(利益),我让他们一成不变?
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?
我看到 查找&LT; TKEY的,TElement&GT;类
也是不可改变的,以为我会错过使用LINQ查询一些好处(?)
如果是这样,你能给我什么样的例子我错过?
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?
推荐答案
第二个问题的答案是,成员变量依然可设置不用只读
关键字 - 只在类本身,但它仍然是可能的。
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
.
相关推荐
最新文章