Game Development Reference
In-Depth Information
public override int GetHashCode()
{
return (int)X ^ (int)Y ^ (int)Z;
}
public static bool operator ==(Vector v1, Vector v2)
{
// If they're the same object or both null, return true.
if (System.Object.ReferenceEquals(v1, v2))
{
return true;
}
// If one is null, but not both, return false.
if (v1 == null || v2 == null)
{
return false;
}
return v1.Equals(v2);
}
public override bool Equals(object obj)
{
if (obj is Vector)
{
return Equals((Vector)obj);
}
return base.Equals(obj);
}
public static bool operator !=(Vector v1, Vector v2)
{
return !v1.Equals(v2);
}
A lot of code just to overload the equality operator! The only curious function is
GetHashCode . A hash is a number that tries, but is not guaranteed, to uniquely
identify an object; it's used in C# Dictionary structures. It needs to be over-
ridden when overriding equality because if equality is overridden, it makes it
harder for the compiler to know what a good hash would be.
 
Search WWH ::




Custom Search