我有类LineSegment,它覆盖了GetHashCode
public class LineSegment : IComparable {
int IComparable.CompareTo(object obj) {
...
}
public override bool Equals(object obj) {
...
}
public override int GetHashCode() {
GD.Print("Hash");
...
}
}我使用LineSegments作为SortedDictionary中的键。
SortedDictionary<LineSegment, bool> sD = new SortedDictionary<LineSegment, bool>();
sD.add(new LineSegment());但是,GetHashCode从未被调用过,即使SortedDictionary.Add()抱怨说“已经添加了一个具有相同键的项”,也是如此。
如果我将SortedDictionary更改为Dictionary,则调用GetHashCode。为什么?
发布于 2022-07-27 12:52:30
SortedDictionary目前使用的是类似红黑树的数据结构,因此,它不只是反复调用CompareTo而不是散列。
https://stackoverflow.com/questions/73138302
复制相似问题