我想從一個Vector2 []中調用時重寫GetHashCode()方法。這段代碼爲我知道的對象生成非唯一散列:我將下面的類傳遞給同一個矩形,並生成不同的散列碼。如何在c中散列一個int []#
public Shape(Rectangle r)
{
edges = new Vector2[4];
edges[0] = new Vector2(0, 0);
edges[1] = new Vector2(r.Width, 0);
edges[2] = new Vector2(r.Width, r.Height);
edges[3] = new Vector2(0, r.Height);
Console.Write(edges.GetHashCode() + "\n");
Position = new Vector2(r.X, r.Y);
}
Vector2數組只是一堆整數。我怎樣才能創建一個獨特的散列爲整數列表?
這可能應該工作。你可以發佈一個完整的例子,顯示兩個相等的向量產生不同的哈希碼? –
數組不提供基於數組內容的哈希碼。所以這段代碼不起作用。你必須推出你自己的,或者如果你在.NET 4上使用[IStructuralEquatable接口](http://msdn.microsoft.com/en-us/library/system.collections.istructuralequatable.aspx)。 –
@SimonWhitehead:真的嗎?那麼[Vector2.GetHashCode](http://msdn.microsoft.com/zh-cn/library/microsoft.xna.framework.vector2.gethashcode%28v=xnagamestudio.10%29.aspx)會返回什麼? –