You should override Equals
and GetHashCode
whenever the default Object.Equals
(tests for reference equality) will not suffice. This happens, for example, when the type of your key is a custom type and you want two keys to be considered equal even in cases when they are not the same instance of the custom type.
For example, if your key is as simple as
class Point {
public int X { get; set; }
public int Y { get; set; }
}
and you want two Point
s two be considered equal if their X
s are equal and their Y
s are equal then you will need to override Equals
and GetHashCode
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…