Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I override the equals operator == for an interface in C#?

Tags:

I have defined the following interface:

public interface IHaveAProblem {     string Issue { get; set; } } 

And here is the implementation of IHaveAProblem:

public class SomeProblem : IHaveAProblem {     public string Issue { get; set; }      public override bool Equals(object obj)     {         SomeProblem otherObj = obj as SomeProblem;          if (otherObj == null)         {             return false;         }          return this.Issue == otherObj.Issue;     }      public override int GetHashCode()     {         return base.GetHashCode();     }          public static bool operator ==(SomeProblem rhs, SomeProblem lhs)     {         // Null check         if (Object.ReferenceEquals(rhs, null) || Object.ReferenceEquals(lhs, null))         {             if (Object.ReferenceEquals(rhs, null) && Object.ReferenceEquals(lhs, null))             {                 // Both are null.  They do equal each other                 return true;             }              // Only 1 is null the other is not so they do not equal             return false;         }          return rhs.Equals(lhs);     }      public static bool operator !=(SomeProblem rhs, SomeProblem lhs)     {         // Null check         if (Object.ReferenceEquals(rhs, null) || Object.ReferenceEquals(lhs, null))         {             if (Object.ReferenceEquals(rhs, null) && Object.ReferenceEquals(lhs, null))             {                 // Both are null.  They do equal each other                 return false;             }              // Only 1 is null the other is not so they do not equal             return true;         }          return !rhs.Equals(lhs);     } } 

When I use the object, I can get the correct results for the == compare:

SomeProblem firstTest = new SomeProblem()     {         Issue = "Hello World"     };  SomeProblem secondTest = new SomeProblem()     {         Issue = "Hello World"     };  // This is true bool result = firstTest == secondTest; 

However, when I try to compare the interfaces, it is doing a memory compare rather than the operator == on SomeProblem:

IHaveAProblem firstProblem = new SomeProblem()     {         Issue = "Hello World"     };  IHaveAProblem secondProblem = new SomeProblem()     {         Issue = "Hello World"     }; 

Is it possible to have the interface use the == on SomeProblem rather than a memory compare?

I know I can do a firstProblem.Equals(secondProblem) and get the proper results. However, I am creating a framework and I will not know how it is used in the end. I thought == would work correctly.

like image 599
David Basarab Avatar asked Dec 10 '09 14:12

David Basarab


People also ask

Should I override equality and inequality operators?

In a class, if you overload the Equals method, you should overload the == and != operators, but it is not required.

How do you override equals method in C#?

Because Complex is a value type, it cannot be derived from. Therefore, the override to Equals(Object) method need not call GetType to determine the precise run-time type of each object, but can instead use the is operator in C# or the TypeOf operator in Visual Basic to check the type of the obj parameter.

Can interface contain operators?

You can't define operators on interfaces because a class can implement multiple interfaces.

Is equal to C#?

The most common way to compare objects in C# is to use the == operator. For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types other than string, == returns true if its two operands refer to the same object.


1 Answers

The operator == is static. You cannot define static methods for interfaces in C#. Also, for all operators at least one of the argument types needs to be of the same type as the class it is defined in, therefore: No operator overloading for interfaces :(

What you CAN do is use an abstract class instead - and define the operator there. Again, the operator may NOT be virtual (since static methods cannot be virtual...)

[Edited, reason see comment.]

like image 66
gha.st Avatar answered Sep 22 '22 16:09

gha.st