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

前端 未结 4 1187

I have defined the following interface

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

And here is the implementation of

4条回答
  •  Happy的楠姐
    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.

提交回复
热议问题