我试图比较Definition
类型的两个哈希集作为EqualityComparer<T>.Default.Equals(value, oldValue)
。 Definition
定义如下检查对象的HashSet是否相等
public class Definition
{
public string Variable { get; set; }
public HashSet<Location> LocationList { get; set; }
public override bool Equals(object obj)
{
Definition other = obj as Definition;
return other.Variable.Equals(this.Variable) && other.LocationList!= null &&this.LocationList != null
&& other.LocationList.Count == this.LocationList.Count
&& other.LocationList == this.LocationList;
}
public override int GetHashCode()
{
return this.Variable.GetHashCode()^this.LocationList.Count.GetHashCode();//^this.LocationList.GetHashCode();
}
}
public class Location
{
public int Line { get; set; }
public int Column { get; set; }
public int Position { get; set; }
public string CodeTab { get; set; }
public Location(int line, int col, int pos, string tab)
{
Line = line;
Column = col;
Position = pos;
CodeTab = tab;
}
public override bool Equals(object obj)
{
Location other = obj as Location;
return this.CodeTab == other.CodeTab
&& this.Position == other.Position
&& this.Column == other.Column
&& this.Line == other.Line;
}
public override int GetHashCode()
{
return this.CodeTab.GetHashCode()^this.Position.GetHashCode()
^this.Column.GetHashCode()^this.Line.GetHashCode();
}
}
不知何故对于类似的一组,其结果是,尽管保持相同的所有信息返回false
。唯一的区别是某些元素的位置互换,但我知道HashSet
在比较时不会保留或检查顺序。任何人都可以告诉我这里出了什么问题吗?
PS:我试着取消注释this.LocationList.GetHashCode()
也没有工作。
这是一个'HashSet的'不是'HashSet的'但你的状态,你想比较_“定义的两个哈希集”_。所以你的问题要么是错误的,要么是你的代码做了一些不同的事情。 –
'value'和'oldValue'是'Definition'类型。 – Sreeja