I have created a Graph Class including HashSet<T>
as Nodes and HashSet<IEdge<T>>
as Edges.
public interface IEdge<T>
{
T A_Node { get; }
T Another_Node { get; }
}
public interface IGraph<T>
{
ISet<T> Nodes { get; }
ISet<IEdge<T>> Edges { get; }
}
I want to avoid adding duplicate Nodes and Edges to my Graph.
Based on this Question :
1) IEqualityComparer is an interface for an object that performs the comparison on two objects of the type T.
2) IEquatable is for an object of type T so that it can compare itself to another.
also
3) If there is only one way of testing two instances of T for equality, or if one of several methods is preferred One instance of T has internal knowledge of how to compare itself to another instance of T.
4) On the other hand, if there are several equally reasonable methods of comparing two Ts for equality, IEqualityComparer would seem more appropriate
5) This interface is not meant to be implemented by T itself, but by other "external" classes.
Based on these tips(1-5) and my code, I need to have IEdge be able to compare itself to another.
Also there is only one way of testing two instances of T or IEdge for equality.
So It seems Implementing IEquatable is the solution.
But the problem is that HashSets use GetHashCode method.
And IEquatable doesn't force to override GetHashCode method.
How can I use IEquatable and in the same time force the developer to override GetHashCode ?!
If you prefer using IEqualityComparer<T>
or EqualityComparer<T>
in this case, doesn't it matter that it has conflict with Tips above ?