“the given key was not present in the dictionary” error when using a self-defined class as key

后端 未结 3 2009
没有蜡笔的小新
没有蜡笔的小新 2021-02-10 11:16

I\'ve got code like this:

if (CounterForEachRelatedTagDict.Select(x => x.Key).Contains(tag.Key))
   CounterForEachRelatedTagDict[tag.Key] += tag.Value;
         


        
3条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-10 12:00

    First:You can use the ContainsKey method instead of that Linq Query.

    Second: You must override the GetHashCode and Equals for MyType. that's the way that Dictionary look up and compare the keys.

    Check out these similar questions: Dictionary.ContainsKey return False, but a want True, Using an object as a generic Dictionary key

提交回复
热议问题