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 |
但是,当我尝试比较接口时,它正在进行内存比较,而不是在某个问题上执行运算符==
1 2 3 4 5 6 7 8 9 |
。
接口是否可能在某个问题上使用==而不是内存比较?
我知道我可以做一个
静电
你能做的是使用抽象类的定义和安instead the operator there。再次,可能not be the operator(因为静态方法不能虚虚嗯…)P></
[评论] edited,reason)。P></
IIRC(我可能是错的,#界面C)不允许overloading算子。P></
但在这房子是好的。normally Maps to the operator =参考平等。你想要它听起来像价值平等,那你想力均值them to the
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></