Possible duplicate:
Can I overload the == operator on an interface?
I understand how to overload operator== for one class. I even did work with inheritance. But I can't seem to implement it for an interface. Imagine the following:
IFoo foo1 = new Foo(42); IFoo foo2 = new Foo(42); Assert.IsTrue( foo1 == foo2 );
As you might have guessed, I want classes that implement IFoo to behave like a value type (at least when comparing for equality). As you can see, the specific type Foo foo1 and foo2 no longer “known” when Assert occurs. All that is known is their IFoo interface.
Of course, I could use IEquatable<IFoo> and write foo1.Equals(foo2) instead, but that is not the point here. And if IFoo was an abstract base class instead of an interface, I could overload operator== without any problems. So how do I do the same for the interface?
The problem basically boils down to the inability to write the following, because at least one of left and right must be Foo :
public interface IFoo : IEquatable<IFoo> { int Id { get; } } public class Foo : IFoo { public int Id { get; private set; } ...
Overloading operator== for an interface is not possible or is there a trick I missed?
Lumpn source share