[英]The property KeyValuePair<TKey, Tvalue>.Value has no setter
I'm using a Dictionary<int, KeyValuePair<bool, int>>
to hold data. 我正在使用
Dictionary<int, KeyValuePair<bool, int>>
来保存数据。
From time to time I need to increment the int
in the KeyValuePair
, but it won't let me, because it has no setter. 我不时需要在
KeyValuePair
增加int
,但它不会让我,因为它没有setter。 Is there a way to increment it? 有没有办法增加它?
Code sample: 代码示例:
Dictionary<int, KeyValuePair<bool, int>> mDictionary =
new Dictionary<int, KeyValuePair<bool, int>>();
mDictionary[trapType].Value++;
//Error: The property KeyValuePair<TKey, Tvalue>>.Value has no setter
Is there a way to increment it?
有没有办法增加它?
No. KeyValuePair
is immutable - it's also a value type, so changing the value of the Value
property after creating a copy wouldn't help anyway. KeyValuePair
是不可变的 - 它也是一个值类型,因此在创建副本后更改Value
属性的Value
无论如何都无济于事。
You'd have to write something like this: 你必须写这样的东西:
var existingValue = mDictionary[trapType];
var newValue = new KeyValuePair<bool, int>(existingValue.Key,
existingValue.Value + 1);
mDictionary[trapType] = newValue;
It's pretty ugly though - do you really need the value to be a KeyValuePair
? 这很丑陋 - 你真的需要价值成为
KeyValuePair
吗?
mDictionary[trapType] = new KeyValuePair<bool, int>(mDictionary[trapType].Key, mDictionary[trapType].Value+1)
KeyValuePair
tends just to be used as an iterator value for dictionaries. KeyValuePair
往往只是用作字典的迭代器值。 You're better off defining your own type if you want it to be mutable. 如果你希望它是可变的,你最好定义自己的类型。
You will need to construct a new key value pair. 您需要构造一个新的键值对。
mDictionary[trapType] = new KeyValuePair<bool,int>(mDictionary[trapType].Key,mDictionary[trapType].Value +1);
However if you are on .NET 4, I'd suggest you use a Tuple
instead of a key value pair if what you are trying to do is simply to have an object that holds two other values. 但是,如果您使用的是.NET 4,我建议您使用
Tuple
而不是键值对,如果您要做的只是拥有一个包含两个其他值的对象。 Ie if there's no key-value relationship between the two. 即如果两者之间没有键值关系。 Then code would then be:
然后代码将是:
mDictionary[trapType] = Tuple.Create(mDictionary[trapType].Item1,mDictionary[trapType].Item2 +1);
another solution is to create a new dictionary and pass the new values and keys to it: 另一个解决方案是创建一个新的字典并将新的值和键传递给它:
foreach (var d in existingDic)
newDic.Add(d.Key, d.Value + 1);
Conceptually, KeyValuePair<TKey,TValue>
is just a pair of variables, Key
and Value
. 从概念上讲,
KeyValuePair<TKey,TValue>
只是一对变量, Key
和Value
。 Had Microsoft implemented it with exposed public fields Key
and Value
, the semantics of it would have been perfect; 如果微软用暴露的公共字段
Key
和Value
实现它,它的语义就会很完美; when used as a ForEach
control variable it would have been immutable, but one could have updated the Key or Value field of an ordinary variable or array element without having to update the other. 当用作
ForEach
控制变量时,它本来是不可变的,但是可以更新普通变量或数组元素的Key或Value字段,而不必更新另一个。
Unfortunately, Microsoft seems to have been unwilling to have framework types expose any public fields, even for types like KeyValuePair<TKey,TValue>
or Drawing.Rectangle
whose semantics dictate that (1) the type must be a struct; 遗憾的是,微软似乎一直不愿意让框架类型暴露任何公共字段,即使是像
KeyValuePair<TKey,TValue>
或Drawing.Rectangle
这样的类型,其语义规定(1)类型必须是结构; (2) its entire state is visible in a fixed set of properties (there may be other computed properties beyond those which define an object's state), and (3) those properties may be assigned any combination of values suitable for their types. (2)其整个状态在一组固定的属性中可见(除了定义对象状态的属性之外,可能还有其他计算属性),以及(3)可以为这些属性分配适合其类型的任何值组合。 Consequently, Microsoft only considered the possibilities of exposing the members comprising the types' state as read-only properties, or as read-write properties.
因此,Microsoft仅考虑了将包含类型状态的成员公开为只读属性或读写属性的可能性。 Using read-write properties would mean that code like:
使用读写属性意味着代码如下:
for each (var item in myDictionary)
item.Value += 1;
or 要么
...assuming MyList is a List<Drawing.Rectangle>
MyList[3].Width += 9;
would be interpreted by the existing C# compiler as either 将被现有的C#编译器解释为
for each (var item in myDictionary)
{ var temp = item; temp .Value += 1; }
or 要么
...assuming MyList is a List<Drawing.Rectangle>
{ temp = MyList[3]; temp.Width += 9; }
both of yield horrible behavior which is almost certainly not what the programmer intended. 产生可怕的行为几乎肯定不是程序员的意图。 The implementers of .net decided that the value of having the members of
KeyValuePair<TKey,TValue>
be individually mutable did not justify the danger posed by the former, but the usefulness of modifying individual members of Rectangle
was sufficient to justify the danger posed by second. .net的实施者认为让
KeyValuePair<TKey,TValue>
的成员具有个体可变性的价值并不能证明前者所构成的危险,但修改Rectangle
个体成员的有用性足以证明由此造成的危险。第二。 Note that neither example would have had to pose any danger had the types used exposed fields rather than properties, since writing to a field of a temporary struct has never been permissible, even when calling property setters was. 请注意,如果类型使用公开的字段而不是属性,则两个示例都不必构成任何危险,因为即使在调用属性setter时,也不允许写入临时结构的字段。
As previously mentioned, the KeyValuePair is immutable. 如前所述,KeyValuePair是不可变的。 I thought it was worth adding here a mutable implementation:
我认为值得在这里添加一个可变的实现:
public class MutableKeyValuePair<KeyType, ValueType>
{
public KeyType Key { get; set; }
public ValueType Value { get; set; }
public MutableKeyValuePair() { }
public MutableKeyValuePair(KeyType key, ValueType val)
{
Key = key;
Value = val;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.