I can't work out how to write my operator overload. Please help!
I have the following class:
public class Nodegrid<N> where N : INode
{
}
Within Nodegrid functions, I want to be able to write things like
N n1;
N n2;
//...
if (n1 == n2)
//...
But I can't work out how to write the == operator overload for N. I tried overloading INode with
public static bool operator ==(INode n1, INode n2)
{
return (n1.X == n2.X && n1.Y == n2.Y);
}
but this wasn't sufficient.
I also tried overloading N itself, but I got compiler errors because it was expecting me to overload Nodegrid, not N.
If this is possible, please provide code, if not, please suggest workarounds!
Thanks Haighstrom
UPDATE
For now I have implemented the following workaround on the basis this cannot be done using operator overloads:
public static class NodeExts
{
public static bool Equals(this INode n1, INode n2)
{
return (n1.X == n2.X && n1.Y == n2.Y);
}
}