如何在C#中覆盖接口的equals运算符==?

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

我定义了以下接口

1
2
3
4
public interface IHaveAProblem
{
    string Issue { get; set; }
}

下面是我要说的问题的实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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);
    }
}

当我使用这个对象时,我可以得到==比较的正确结果。

1
2
3
4
5
6
7
8
9
10
11
12
SomeProblem firstTest = new SomeProblem()
    {
        Issue ="Hello World"
    };

SomeProblem secondTest = new SomeProblem()
    {
        Issue ="Hello World"
    };

// This is true
bool result = firstTest == secondTest;

但是,当我尝试比较接口时,它正在进行内存比较,而不是在某个问题上执行运算符==

1
2
3
4
5
6
7
8
9
IHaveAProblem firstProblem = new SomeProblem()
    {
        Issue ="Hello World"
    };

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

接口是否可能在某个问题上使用==而不是内存比较?

我知道我可以做一个firstProblem.Equals(secondProblem)并得到正确的结果,但是我正在创建一个框架,我不知道它最终是如何使用的。我想==能正常工作。


静电==is the operator。你不能定义静态方法# for C接口。也,在least for one of the argument经营者types of needs to be the same as the class is型定义:恩,therefore社(不overloading for界面:P></

你能做的是使用抽象类的定义和安instead the operator there。再次,可能not be the operator(因为静态方法不能虚虚嗯…)P></

[评论] edited,reason)。P></


IIRC(我可能是错的,#界面C)不允许overloading算子。P></

但在这房子是好的。normally Maps to the operator =参考平等。你想要它听起来像价值平等,那你想力均值them to the .Equals()重写(和consequently也.GetHashCode())函数。你有你的inherit from that by IEquatable()接口。P></


konw this is an的老问题,但在秀林,how to出现两类提供了一个点的情况下,与不出现的情况下,how to双接口。P></

在一些案例中,this is the Way to dryest界面出现。P></

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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();
    }
}

使用?P></

1
2
3
4
5
6
7
8
9
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

你有想IComparable使然?P></

这样:P></

1
2
3
4
public interface IHaveAProblem : IComparable
{
    string Issue { get; set; }
}

然后在茶类:implementation of theP></

1
2
3
4
5
6
7
8
9
10
11
public class SomeProblem : IHaveAProblem
{
    public string Issue { get; set; }

    ...

    public int CompareTo(object obj)
    {
        return Issue.CompareTo(((SomeProblem)obj).Issue);
    }
}

这说明,当你的作品,只出现两情况下,but not any other of someproblem,implementations of the ihaveaproblem接口。P></

if not sure there我在occur NullReferenceException。P></