我正在尝试为我的类创建自定义比较器:
```html
但是看起来它从未以这种方式使用我的EdgeComparer。我做错了什么?
```html
我正在尝试为我的类创建自定义比较器:
```using System.Collections.Generic;
namespace i.changed.namespaces.DataStructures
{
public class Edge
{
public Cords startPoint, endPont;
public double length;
//more code here that doesnt matter
}
public class EdgeComparer : IEqualityComparer<Edge>
{
public bool Equals(Edge x, Edge y)
{
//Check whether the objects are the same object.
if (x.Equals(y)) return true;
return x.startPoint.Equals(y.startPoint) && x.endPont.Equals(y.endPont) && (x.length - y.length < 0.0001);
}
public int GetHashCode(Edge obj)
{
int hash = 17;
hash = hash * 23 + obj.length.GetHashCode();
hash = hash * 23 + obj.startPoint.GetHashCode();
hash = hash *23 + obj.endPont.GetHashCode();
return hash;
}
}
我正在另一个对象中使用这个类:
using i.changed.namespaces.DataStructures;
namespace i.changed.namespaces
public class MyClass
{
HashSet<Edge> Edges, NewEdges;
public MyClass()
{
NewEdges = new HashSet<Edge>();
Edges = new HashSet<Edge>();
}
在某个时候,我希望获得这些哈希集合的并集:
newEdges.UnionWith(Edges);
但是看起来它从未以这种方式使用我的EdgeComparer。我做错了什么?