I have such a class:
public class Cycle
{
public List<int> Edges
{
get;
private set;
}
public override bool Equals(object obj)
{
Cycle cycle = (Cycle)obj;
var list1 = cycle.Edges;
var list2 = Edges;
var same = list1.Except(list2).Count() == 0 &&
list2.Except(list1).Count() == 0;
return same;
}
public override int GetHashCode()
{
// return Edges.GetHashCode();
}
}
As you can see, if two Edge
Lists are the same, then I deem the Cycles
as the same.
The issue now is how to implement the GetHashCode()
?
I tried Edges.GetHashCode()
, but the problem is that two List<Cycle>
, with the same Cycle
object but different orders, will be deemed different, even though they should be the same.
Except
is a set operation which gives you only the distinct items. If distinct items are all that matters then may be usingHashSet<T>
is a better choice. It hasHashSet<T>.CreateSetComparer
which does all this for free. In any case if you want to compare the distinct items only for equality, then!Any
is more performant thanCount == 0
. Like!list1.Except(list2).Any() && !list2.Except(list1).Any();
. – Noelyn