简体   繁体   English

在C#中为类实现==运算符的最佳实践是什么?

[英]What are the best practices for implementing the == operator for a class in C#?

While implementing an == operator, I have the feeling that I am missing some essential points. 在实现一个==运算符时,我觉得我缺少一些基本要点。
Hence, I am searching some best practices around that. 因此,我正在寻找一些最佳实践。
Here are some related questions I am thinking about: 以下是我正在考虑的一些相关问题:

  • How to cleanly handle the reference comparison? 如何干净地处理参考比较?
  • Should it be implemented through a IEquatable<T> -like interface? 它应该通过IEquatable<T>类接口实现吗? Or overriding object.Equals ? 或者重写object.Equals
  • And what about the != operator? 那么!=运算符呢?

(this list might not be exhaustive). (此列表可能并非详尽无遗)。

I would follow Microsoft's Guidelines for Overloading Equals() and Operator == . 我会遵循Microsoft的重载等于()和运算符==指南

edit : Microsoft's guidelines contain this important remark, which seems to confirm Henk's answer: 编辑 :微软的指南包含这个重要的评论,这似乎证实了Henk的答案:

By default, the operator == tests for reference equality by determining if two references indicate the same object, so reference types do not need to implement operator == in order to gain this functionality. 默认情况下,operator ==通过确定两个引用是否指示相同的对象来测试引用相等性,因此引用类型不需要实现operator ==以获得此功能。 When a type is immutable, meaning the data contained in the instance cannot be changed, overloading operator == to compare value equality instead of reference equality can be useful because, as immutable objects, they can be considered the same as long as they have the same value. 当一个类型是不可变的,意味着实例中包含的数据不能被改变时,重载operator ==来比较值相等而不是引用相等可能是有用的,因为作为不可变对象,只要它们具有它们就可以被认为是相同的相同的价值。 Overriding operator == in non-immutable types is not recommended 不建议在非不可变类型中覆盖operator ==

Each time you implement the == operator, be sure to also implement != , IEquatable<T> and to override Object.Equals() and Object.GetHashCode() for consistency for the user of your class. 每次实现==运算符时,一定要实现!=IEquatable<T>并覆盖Object.Equals()Object.GetHashCode()以保证类的用户的一致性。

Considering a class, here's my usual implementation: 考虑到一个类,这是我通常的实现:

    public bool Equals(MyClass other) {
        if (ReferenceEquals(other, null))
            return false;
        if (ReferenceEquals(other, this))
            return true;
        return // your equality code here
    }

    public override bool Equals(object obj) {
        return Equals(obj as MyClass);
    }

    public override int GetHashCode() {
        return // your hash function here
    }

    public static bool operator ==(MyClass left, MyClass right) {
        return Equals(left, right);
    }

    public static bool operator !=(MyClass left, MyClass right) {
        return !(left == right);
    }
  • If you implement ==, override .Equals and . 如果你实现==,覆盖.Equals和。 GetHashCode GetHashCode的
  • implement != as well 实施!=同样
  • Check for null references using object.ReferenceEquals otherwise the operator will recurse 使用object.ReferenceEquals检查空引用,否则运算符将递归

The most common approach is not to handle it. 最常见的方法是处理它。 The default is reference comparison which in general is right for class objects. 默认值是引用比较,通常适用于类对象。

So first you want to be very sure you need value-type behaviour. 所以首先你要确定你需要价值型行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM