C# 如何修改KeyValuePair值?

C# 如何修改KeyValuePair值?,c#,.net,C#,.net,我在尝试修改项的值时遇到问题,因为它只是一个只读字段 KeyValuePair<Tkey, Tvalue> KeyValuePair 我尝试过不同的选择,比如: Dictionary<Tkey, Tvalue> 字典 但我也有同样的问题。有没有办法将值字段设置为新值?KeyValuePair是不可变的。您需要使用修改过的键或值创建一个新的。下一步实际做什么取决于您的场景,以及您到底想做什么…您不能修改它,您可以用一个新的替换它 var newEntry = new

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

KeyValuePair<Tkey, Tvalue>
KeyValuePair
我尝试过不同的选择,比如:

Dictionary<Tkey, Tvalue>
字典

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

KeyValuePair
是不可变的。您需要使用修改过的键或值创建一个新的。下一步实际做什么取决于您的场景,以及您到底想做什么…

您不能修改它,您可以用一个新的替换它

var newEntry = new KeyValuePair<Tkey, Tvalue>(oldEntry.Key, newValue);

这里,如果您想使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;
    }
}
公共类KeyVal
{
公钥Id{get;set;}
公共值文本{get;set;}
public KeyVal(){}
公钥Val(Key,Val)
{
Id=key;
this.Text=val;
}
}

因此,我们可以在KeyValuePair中的任何位置使用它。

您不能修改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;
};

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();
  }
}
namespace System.Collections.Generic
{
[可序列化]
公共结构KeyValuePair
{
公钥值对(TKey key,TValue value);
公钥{get;}
公共TValue值{get;}
公共重写字符串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));
}
var list=newlist();
添加(新的KeyValuePair(“Cat”,1));
添加(新的KeyValuePair(“Dog”,2));
添加(新的KeyValuePair(“兔子”,4));
int removalStatus=list.RemoveAll(x=>x.Key==“Rabbit”);
如果(移除状态==1)
{
添加(新的KeyValuePair(“兔子”,5));
}
键值对
是一个结构,C#中的结构是值类型,并且被提到是不可变的。原因很明显,
字典
应该是一个高性能的数据结构。使用引用类型而不是值类型会占用太多内存开销。与字典中直接存储的值类型不同,此外,字典中每个条目的32位或64位引用都将被分配。这些引用将指向条目实例的堆。总体业绩将迅速下降

Microsoft选择struct而不是满足
Dictionary
的类的规则:

✔️ 如果类型的实例很小,通常是短命的,或者通常嵌入在其他对象中,请考虑定义结构而不是类。 ❌ 除非类型具有以下所有特征,否则避免定义结构:

  • 它在逻辑上表示单个值,类似于基本类型(int、double等)
  • 它的实例大小小于16字节
  • 它是不变的
  • 它不必经常装箱
字典_rowItems=新字典();
_其中(x=>x.Value>1).ToList().ForEach(x=>{u rowItems[x.Key]=x.Value-1;});
对于字典,我们可以根据某些条件以这种方式更新值


您是在尝试更新特定值,还是词典中的所有/大部分值?我尝试更新特定值。谢谢您的帮助。字典的部分正是我需要的。我可以修改KeyValuePair。永远不要低估P/Invoke。
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));
}
Dictionary<long, int> _rowItems = new Dictionary<long, int>();
  _rowItems.Where(x => x.Value > 1).ToList().ForEach(x => { _rowItems[x.Key] = x.Value - 1; });