Suppose I have an interface IFoo, and a class Foo that implements IFoo. I want to define my own logic for determining if 2 instances of Foo are equal based on the data they contain and overload the == & != operators for ease of use when working with Foo.
If I have 2 instances of Foo, both stored as Foo, then this works fine. However, if I have 2 instances both stored as IFoo, suddenly my overloaded operators aren't being called anymore. Furthermore, since I can't define an operator on my IFoo interface and at least one of the arguments in my Foo operator overload must be of type Foo, I can't see that there's any way I can successfully overload the operator on my type. Is this correct?
Also, can anyone clear up why this is happening? Normally I'd expect that the fact my Foos are being stored as IFoos should be irrelevant in determining which equality function gets called, since fundamentally they're still Foos!
Any help really appreciated
Thanks
Edit: Ok, since there seems to be a bit of confusion of what I mean, here's an example which should hopefully clarify a bit:
public interface IFoo
{
}
public class Foo : IFoo
{
public static bool operator==(Foo left, Foo right)
{
....
}
}
Foo foo1 = new Foo();
Foo foo2 = new Foo();
bool comparison1 = foo1 == foo2 //This is successfully calling the overloaded operator
IFoo ifoo1 = foo1;
IFoo ifoo2 = foo2;
bool comparison2 = ifoo1 == ifoo2 //This isn't
static). The only workarounds I've seen are to use methods instead of operators. For example,myFoo.Add(otherFoo)instead ofmyFoo + otherFoo