In the code below, I was wondering why XOR (^) is being used to combine the hascodes of the constituent members of the composition (this is source from MonoCross 1.3)?
Is the bitwise XOR of an
MXViewPerspectiveobject'sPerspectiveandModelTypemember's used to uniquely identify the instance ?If so, is there a name for this property of the XOR operation (how XOR-ing two values (ie, hashcodes) guarantees uniqueness) ?
public class MXViewPerspective : IComparable
{
public MXViewPerspective(Type modelType, string perspective)
{
this.Perspective = perspective;
this.ModelType = modelType;
}
public string Perspective { get; set; }
public Type ModelType { get; set; }
public int CompareTo(object obj)
{
MXViewPerspective p =(MXViewPerspective)obj;
return this.GetHashCode() == p.GetHashCode() ? 0 : -1;
}
public static bool operator ==(MXViewPerspective a, MXViewPerspective b)
{
return a.CompareTo(b) == 0;
}
public static bool operator !=(MXViewPerspective a, MXViewPerspective b)
{
return a.CompareTo(b) != 0;
}
public override bool Equals(object obj)
{
return this == (MXViewPerspective)obj;
}
public override int GetHashCode()
{
return this.ModelType.GetHashCode() ^ this.Perspective.GetHashCode();
}
public override string ToString()
{
return string.Format("Model \"{0}\" with perspective \"{1}\"", ModelType, Perspective);
}
}
Thank you.