在下面的代码中,我想知道为什么使用 XOR (^) 来组合组合的组成成员的 hascode(这是来自 MonoCross 1.3 的源代码)?
MXViewPerspective
对象Perspective
和成员的按位异或是否ModelType
用于唯一标识实例?如果是这样,XOR 操作的这个属性是否有一个名称(XOR-ing 两个值(即哈希码)如何保证唯一性)?
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);
}
}
谢谢你。