.net中5种相等检查的方法..为什么?以及使用哪个? [英] 5 ways for equality check in .net .. why? and which to use?

查看:103
本文介绍了.net中5种相等检查的方法..为什么?以及使用哪个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学习.net(通过C#)的同时,我发现了5种检查对象之间相等性的方法。

While learning .net (by c#) i found 5 ways for checking equality between objects.


  1. ReferenceEquals()方法。 / li>
  2. 虚拟Equals()方法。 (系统对象)

  3. 静态Equals()方法。

  4. 来自IEquatable接口的Equals方法。

  5. 比较运算符==。

  1. The ReferenceEquals() method.
  2. The virtual Equals() method. (System.Object)
  3. The static Equals() method.
  4. The Equals method from IEquatable interface.
  5. The comparison operator == .

我的问题是:


  1. 为什么有这么多的Equals()方法以及比较运算符?

  2. 应该使用虚拟Equals()或IEquatable的Equals()之一。 (例如,如果我们使用自己的集合类)


推荐答案

1-引用等于检查两个引用类型变量(类,而不是结构)被引用到相同的内存地址。

1 - Reference equals checks if two reference type variables(classes, not structs) are referred to the same memory adress.

2-虚拟Equals()方法检查两个对象是否相等。假设您有此类:

2 - The virtual Equals() method checks if two objects are equivalent. Let us say that you have this class:

class TestClass{
    public int Property1{get;set}
    public int Property2{get;set}

    public override bool Equals(object obj)
    {
        if (obj.GetType() != typeof(TestClass))
            return false;

        var convertedObj = (TestClass)obj;

        return (convertedObj.Property1 == this.Property1 && convertedObj.Property2 == this.Property2);
    }
}

,您实例化了该类中的2个对象:

and you instantiate 2 objects from that class:

var o1 = new TestClass{property1 = 1, property2 = 2}
var o2 = new TestClass{property1 = 1, property2 = 2}

尽管两个对象不是TestClass的相同实例,但对o1的调用。 Equals(o2)将返回true。

although the two objects are not the same instance of TestClass, the call to o1.Equals(o2) will return true.

3-静态Equals方法用于在检查中存在空值时处理问题。
想象一下,例如:

3 - The static Equals method is used to handle problems when there is a null value in the check. Imagine this, for instance:

TestClass o1 = null;
var o2 = new TestClass{property1 = 1, property2 = 2}

如果尝试

o1.Equals(o2);

您将获得NullReferenceException,因为o1指向空。
要解决此问题,请执行以下操作:

you wil get a NullReferenceException, because o1 points to nothing. To adress this issue, you do this:

Object.Equals(o1,o2);

Object.Equals(o1,o2);

该方法准备处理空引用。

This method is prepared to handle null references.

4-IEquatable接口由.Net提供,因此您无需在Equals方法内进行强制转换。
如果编译器发现您已在要检查相等性的类型的类中实现了接口,则它将使该方法优先于Object.Equals(Object)重写。
例如:

4 - The IEquatable interface is provided by .Net so you don't need to do casts inside your Equals method. If the compiler finds out that you have implemented the interface in a class for the type you are trying to check for equality, it will give that method priority over the Object.Equals(Object) override. For instance:

class TestClass : IEquatable<TestClass>
{
    public int Property1 { get; set; }
    public int Property2 { get; set; }

    public override bool Equals(object obj)
    {
        if (obj.GetType() != typeof(TestClass))
            return false;

        var convertedObj = (TestClass)obj;

        return (convertedObj.Property1 == this.Property1 && convertedObj.Property2 == this.Property2);
    }

    #region IEquatable<TestClass> Members

    public bool Equals(TestClass other)
    {
        return (other.Property1 == this.Property1 && other.Property2 == this.Property2);
    }

    #endregion
}

现在如果我们这样做:

var o1 = new TestClass{property1 = 1, property2 = 2}
var o2 = new TestClass{property1 = 1, property2 = 2}
o1.Equals(o2);

被调用的方法是Equals(TestClass),在Equals(Object)之前。

The called method is Equals(TestClass), prior to Equals(Object).

5-==运算符通常表示与ReferenceEquals相同,它检查两个变量是否指向相同的内存地址。
需要注意的是,可以重写此运算符以执行其他类型的检查。
例如,在字符串中,它检查两个不同的实例是否等效。

5 - The == operator usually means the same as ReferenceEquals, it checks if two variables point to the same memory adress. The gotcha is that this operator can be overrided to perform other types of checks. In strings, for instance, it checks if two different instances are equivalent.

这是一个有用的链接,可以更好地理解.Net中的相等性:

This is a usefull link to understand equalities in .Net better:

  • CodeProject

这篇关于.net中5种相等检查的方法..为什么?以及使用哪个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆