【发布时间】:2022-07-27 20:52:30
【问题描述】:
我有课程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 被 调用。为什么?
【问题讨论】:
标签: c#