【问题标题】:How to modify a KeyValuePair value?如何修改 KeyValuePair 值?
【发布时间】:2012-11-07 10:18:05
【问题描述】:

当我尝试修改项目的值时遇到问题,因为它只是一个只读字段。

KeyValuePair<Tkey, Tvalue>

我尝试了不同的选择,例如:

Dictionary<Tkey, Tvalue>

但我也有同样的问题。有没有办法将值字段设置为新值?

【问题讨论】:

  • 您是要更新字典中的特定值还是所有/大部分值?
  • 我尝试更新一个特定的值。

标签: c# .net key-value


【解决方案1】:

KeyValuePair&lt;TKey, TValue&gt; 是不可变的。您需要使用修改后的键或值创建一个新的。你接下来实际做什么取决于你的场景,以及你到底想做什么......

【讨论】:

  • 我可以修改 KeyValuePair。永远不要低估 P/Invoke。
【解决方案2】:

不能修改,可以换一个新的。

var newEntry = new KeyValuePair<TKey, TValue>(oldEntry.Key, newValue);

或字典:

dictionary[oldEntry.Key] = newValue;

【讨论】:

  • 感谢您的帮助。字典的部分正是我需要的。
【解决方案3】:

这里,如果你想让 KeyValuePair 可变。

制作一个自定义类。

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;
    }
}

所以我们可以让它在 KeyValuePair 中的任何地方使用。

【讨论】:

    【解决方案4】:

    您不能修改 KeyValuePair,但可以像这样修改字典值:

    foreach (KeyValuePair<String, int> entry in dict.ToList())
    {
        dict[entry.Key] = entry.Value + 1;
    }
    

    或者像这样:

    foreach (String entry in dict.Keys.ToList())
    {
        dict[entry] = dict[entry] + 1;
    };
    

    【讨论】:

      【解决方案5】:

      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();
        }
      }
      

      如果您想要更新 KeyValuePair 中的任何现有值,那么您可以尝试删除现有值,然后添加修改后的值

      举个例子:

      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));
      }
      

      【讨论】:

        【解决方案6】:

        KeyValuePair&lt;TKey, TValue&gt; 是一个结构体,而 C# 中的结构体是值类型,被提到是不可变的。原因很明显,Dictionary&lt;TKey,TValue&gt; 应该是一个高性能的数据结构。使用引用类型而不是值类型会使用过多的内存开销。与字典中直接存储的值类型不同,此外,将为字典中的每个条目分配 32 位或 64 位引用。这些引用将指向入口实例的堆。整体性能会迅速下降。

        Microsoft 规则选择结构而不是 Dictionary&lt;TKey,TValue&gt; 满足的类:

        ✔️ 如果类型的实例很小且通常短命或通常嵌入其他对象中,请考虑定义结构而不是类。

        ❌ 避免定义结构,除非该类型具有以下所有特征:

        • 它在逻辑上表示单个值,类似于原始类型(int、double 等)。
        • 实例大小小于 16 字节。
        • 它是不可变的。
        • 不必经常装箱。

        【讨论】:

          【解决方案7】:
          Dictionary<long, int> _rowItems = new Dictionary<long, int>();
            _rowItems.Where(x => x.Value > 1).ToList().ForEach(x => { _rowItems[x.Key] = x.Value - 1; });
          

          对于字典,我们可以根据某些条件以这种方式更新值。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-03-29
            • 1970-01-01
            • 1970-01-01
            • 2014-10-07
            • 1970-01-01
            • 2017-12-28
            相关资源
            最近更新 更多