C# 字典构造函数:System.ArgumentException:已添加具有相同键的项
使用常规字典构造函数从另一个缓存初始化缓存。这是我第一次得到一份工作 System.ArgumentException:已添加具有相同密钥的项C# 字典构造函数:System.ArgumentException:已添加具有相同键的项,c#,dictionary,C#,Dictionary,使用常规字典构造函数从另一个缓存初始化缓存。这是我第一次得到一份工作 System.ArgumentException:已添加具有相同密钥的项 私有字典缓存; 私有void updateCache(字典其他缓存) { cache=newdictionary(otherCache);//引发异常 } 我验证了cache和othercache确实具有相同的类型 这怎么可能 它不是,除非另一个缓存ha继承自允许重复的字典。您可以执行GroupBy键和计数,并查看哪个键导致它抛出吗?Linq在即时窗口
私有字典缓存;
私有void updateCache(字典其他缓存)
{
cache=newdictionary(otherCache);//引发异常
}
我验证了cache和othercache确实具有相同的类型
这怎么可能 它不是,除非另一个缓存ha继承自允许重复的字典。您可以执行GroupBy键和计数,并查看哪个键导致它抛出吗?Linq在即时窗口中不起作用。我将尝试修改代码(不是我的代码)是的,您需要了解副本是如何进入
otherCache
的您是否检查过“otherCache”是否具有默认比较器?
private Dictionary<uint, Dictionary<ClassA, ClassB>> cache;
private void updateCache(Dictionary<uint, Dictionary<ClassA, ClassB>> otherCache)
{
cache = new Dictionary<uint, Dictionary<ClassA, ClassB>>(otherCache); // Raises the exception
}