我正在使用一个Dictionary<int, KeyValuePair<bool, int>>
保存数据。
有时我需要增加int
in the KeyValuePair
,但它不会让我这样做,因为它没有设置器。有办法增加吗?
代码示例:
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
有办法增加吗?
No. KeyValuePair
是不可变的 - 它也是一种值类型,因此更改Value
财产创建副本后无论如何也没有帮助。
你必须写这样的东西:
var existingValue = mDictionary[trapType];
var newValue = new KeyValuePair<bool, int>(existingValue.Key,
existingValue.Value + 1);
mDictionary[trapType] = newValue;
虽然它很丑-你觉得吗really需要该值是KeyValuePair
?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)