C# 如何修改 KeyValuePair 值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13454721/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to modify a KeyValuePair value?
提问by Kimbo
I got a problem when I try to modify the value of an item because its only a read only field.
当我尝试修改项目的值时遇到问题,因为它只是一个只读字段。
KeyValuePair<Tkey, Tvalue>
I've tried different alternatives like:
我尝试了不同的选择,例如:
Dictionary<Tkey, Tvalue>
but there I have the same problem. Is there a way to set the value field to an new value?
但我有同样的问题。有没有办法将值字段设置为新值?
采纳答案by deerchao
You can't modify it, you can replace it with a new one.
你不能修改它,你可以用一个新的替换它。
var newEntry = new KeyValuePair<Tkey, Tvalue>(oldEntry.Key, newValue);
or for dictionary:
或字典:
dictionary[oldEntry.Key] = newValue;
回答by thecoop
KeyValuePair<TKey, TValue>is immutable. You need to create a new one with the modifified key or value. What you actually do next depends on your scenario, and what exactly you want to do...
KeyValuePair<TKey, TValue>是不可变的。您需要使用修改后的键或值创建一个新的。你接下来实际做什么取决于你的场景,以及你到底想做什么......
回答by maulin shah
Here, if you want to make KeyValuePair mutable.
在这里,如果您想让 KeyValuePair 可变。
Make a custom class.
制作自定义类。
public class KeyVal<Key, Val>
{
    public Key Id { get; set; }
    public Val Text { get; set; }
    public KeyVal() { }
    public KeyVal(Key key, Val val)
    {
        this.Id = key;
        this.Text = val;
    }
}
so we can make it use in wherever in KeyValuePair.
所以我们可以在 KeyValuePair 的任何地方使用它。
回答by live-love
You cannot modify a KeyValuePair, but you can modify your dictionary values like this:
您不能修改 KeyValuePair,但可以像这样修改字典值:
foreach (KeyValuePair<String, int> entry in dict.ToList())
{
    dict[entry.Key] = entry.Value + 1;
}
or like this:
或者像这样:
foreach (String entry in dict.Keys.ToList())
{
    dict[entry] = dict[entry] + 1;
};
回答by Sunny
KeyValuePair is immutable,
KeyValuePair 是不可变的,
namespace System.Collections.Generic
{
  [Serializable]
  public struct KeyValuePair<TKey, TValue>
  {
    public KeyValuePair(TKey key, TValue value);
    public TKey Key { get; }
    public TValue Value { get; }
    public override string ToString();
  }
}
If you have any existing value in KeyValuePair that you want to update, then You can try to remove the existing value and then add the modified value
如果您要更新 KeyValuePair 中的任何现有值,那么您可以尝试删除现有值,然后添加修改后的值
For an example:
例如:
var list = new List<KeyValuePair<string, int>>();
list.Add(new KeyValuePair<string, int>("Cat", 1));
list.Add(new KeyValuePair<string, int>("Dog", 2));
list.Add(new KeyValuePair<string, int>("Rabbit", 4));
int removalStatus = list.RemoveAll(x => x.Key == "Rabbit");
if (removalStatus == 1)
{
    list.Add(new KeyValuePair<string, int>("Rabbit", 5));
}
回答by Ondrej Rozinek
The KeyValuePair<TKey, TValue>is a struct and struct in C# is value type and mentioned to be immutable. The reason is obvious, the Dictionary<TKey,TValue>should be a high-performance data structure. Using a reference type instead of value type would use too much memory overhead. Unlike a directly stored value type in a dictionary, in addition the 32bit or 64bit references for each entry in the dictionary would be allocated. These references would be pointed to heap for entry instance. Overall performance would be decreased rapidly.
的KeyValuePair<TKey, TValue>是C#中的结构和结构是值类型和所提为不可变的。原因很明显,Dictionary<TKey,TValue>应该是高性能的数据结构。使用引用类型而不是值类型会使用过多的内存开销。与字典中直接存储的值类型不同,另外将为字典中的每个条目分配 32 位或 64 位引用。这些引用将指向入口实例的堆。整体性能会迅速下降。
The Microsoft rules for choosing struct over class which Dictionary<TKey,TValue>satisfies:
Microsoft 选择 struct 而非 class 的规则Dictionary<TKey,TValue>满足:
?? CONSIDER defining a struct instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.
?? 如果类型的实例很小且通常寿命很短或通常嵌入其他对象中,请考虑定义结构而不是类。
? AVOID defining a struct unless the type has all of the following characteristics:
? 避免定义结构,除非该类型具有以下所有特征:
- It logically represents a single value, similar to primitive types (int, double, etc.).
 - It has an instance size under 16 bytes.
 - It is immutable.
 - It will not have to be boxed frequently.
 
- 它在逻辑上表示单个值,类似于原始类型(int、double 等)。
 - 它的实例大小低于 16 字节。
 - 它是不可变的。
 - 它不必经常装箱。
 

