Custom Class used as key in Dictionary but key not found

前端 未结 4 720
忘了有多久
忘了有多久 2020-12-19 02:03

I have a class, show below, which is used as a key in a Dictionary I\'m having issues when trying to find any key within this diction

4条回答
  •  暖寄归人
    2020-12-19 02:34

    You have not overridden Equals and GetHashCode. You have implemented a second class which can serve as an EqualityComparer. If you don't construct the Dictionary with the EqualityComparer, it will not be used.

    The simplest fix would be to override GetHashCode and Equals directly rather than implementing a comparer (comparers are generally only interesting when you need to supply multiple different comparison types (case sensitive and case insensitive for example) or when you need to be able to perform comparisons on a class which you don't control.

提交回复
热议问题