實現平等我有簡單的類定義2D線:二維線
public class Line {
public double X1 { get; set; }
public double Y1 { get; set; }
public double X2 { get; set; }
public double Y2 { get; set; }
}
我的主要目標是讓從列表中使用.Distinct()不同線路。在我的情況下,如果兩個線的座標相等而不論方向如何(線1,2 - > 3,4等於3,4 - > 1,2),則兩線相等。我要實現equals,如:
public override bool Equals(object obj) {
if (obj as Line == null) { return false; }
var second = (Line)obj;
if (this.X1 != second.X1 && this.X1 != second.X2) { return false; }
if (this.Y1 != second.Y1 && this.Y1 != second.Y2) { return false; }
if (this.X2 != second.X2 && this.X2 != second.X1) { return false; }
if (this.Y2 != second.Y2 && this.Y2 != second.Y1) { return false; }
return true;
}
,但我不知道如何實現GetHashCode的(因爲我知道這是必要的,使其在使用的情況下,鮮明的())
也許你可以使用的IEqualityComparer? http://stackoverflow.com/questions/13331985/how-do-i-use-a-custom-comparer-with-the-linq-distinct-method – Gusman
[C#中的GetHashCode指引](http:// stackoverflow.com/questions/462451/gethashcode-guidelines-in-c-sharp) –
如果你一直使它返回0,它會工作,但性能會成問題。時間複雜度將等於'O(n)'。你可以讓散列碼通過一些公式傳遞參數,並返回結果。它將大大提高性能。例如'返回X1 + X2 + Y1 + Y2'。或者您可以考慮訂單,例如'X1 + X2 << 1 + Y1 << 2 + Y2 << 3'。請注意,如果您讓公式具有較少的散列衝突,則性能會更好。哈希碰撞意味着兩個不同的對象返回相同的哈希碼。順便說一句,生成哈希碼的算法也影響性能 –