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

前端 未结 4 1186

I have defined the following interface

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

And here is the implementation of

相关标签:
4条回答
  • 2021-01-03 23:15

    Have you tried implementing IComparable?

    Like this:

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

    And then in the implementation of the class:

    public class SomeProblem : IHaveAProblem
    {
        public string Issue { get; set; }
    
        ...
    
        public int CompareTo(object obj)
        {
            return Issue.CompareTo(((SomeProblem)obj).Issue);
        }
    }
    

    Note that, this works only when you compare two instances of SomeProblem, but not any other implementations of the IHaveAProblem interface.

    Not sure if there could occur a NullReferenceException.

    0 讨论(0)
  • 2021-01-03 23:19

    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.]

    0 讨论(0)
  • 2021-01-03 23:33

    IIRC (and I could be wrong here), C# interfaces don't allow operator overloading.

    But in this case that's okay. The == operator normally maps to reference equality. It sounds like you want value equality, and that means you want to force them to override the .Equals() (and consequently also .GetHashCode()) functions. You do that by having your interface inherit from IEquatable().

    0 讨论(0)
  • 2021-01-03 23:38

    I konw, this is an old question, but all examples provided show how to compare two class instances, and no one points out how to compare two interface instances.

    In some cases, this is the DRYest way to compare interfaces.

    public interface IHaveAProblem
    {
        string Issue { get; set; }
    }
    
    public class IHaveAProblemComparer : IComparer<IHaveAProblem>, IEqualityComparer<IHaveAProblem>
    {
        public int Compare(IHaveAProblem x, IHaveAProblem y)
        {
            return string.Compare(x.Issue, y.Issue);
        }
    
        public bool Equals(IHaveAProblem x, IHaveAProblem y)
        {
            return string.Equals(x.Issue, y.Issue);
        }
    
        public int GetHashCode(IHaveAProblem obj)
        {
            return obj.GetHashCode();
        }
    }
    

    Usage?

    IHaveAProblemComparer comparer = new IHaveAProblemComparer();
    
    List<IHaveAProblem> myListOfInterfaces = GetSomeIHaveAProblemObjects();
    myListOfInterfaces.Sort(comparer); // items ordered by Issue
    
    IHaveAProblem obj1 = new SomeProblemTypeA() { Issue = "Example1" };
    IHaveAProblem obj2 = new SomeProblemTypeB() { Issue = "Example2" };
    
    bool areEquals = comparer.Equals(obj1, obj2); // False
    
    0 讨论(0)
提交回复
热议问题