什么是“最佳实践”用于比较引用类型的两个实例? [英] What is "Best Practice" For Comparing Two Instances of a Reference Type?

查看:94
本文介绍了什么是“最佳实践”用于比较引用类型的两个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近碰到过这个问题,直到现在,我一直很高兴地重写了相等运算符( == )和/或等于方法,类型实际上包含相同的数据(即两个不同的实例看起来是一样的)。



我一直使用这个



在查看某些 MSDN中的编码标准指南我遇到了一个文章。现在我明白为什么文章是这样说的(因为它们不是相同的实例),但它不回答这个问题:


  1. 比较两种引用类型的最佳方式是什么?

  2. 应该实现 IComparable ? (我也已经提到,这应该保留为值类型)。

  3. 是否有一些我不知道的接口?


  4. $ b

    很多感谢^ _ ^



    更新



    看起来我误读了一些文档(这是一个漫长的一天),并覆盖了等于可能是去的方式..



    < blockquote>

    如果你正在实现引用
    类型,你应该考虑重载
    Equals方法在引用类型
    上,如果你的类型看起来像一个基类型
    如Point,String,BigNumber,
    等。大多数引用类型应该
    不会重载 equality 运算符,
    甚至(如果它们覆盖Equals )。然而,
    如果你正在实现一个引用
    类型,意图具有值
    语义,例如一个复数
    类型,你应该重写相等
    运算符。



    解决方案

    看起来你是用C#编写的,你的类应该实现,如果你想比较两个对象使用一些其他指标,这两个指针(因为对象句柄只是那个,指针)到同一内存地址?。



    我从这里获取了一些示例代码

      class TwoDPoint:System.Object 
    {
    public readonly int x,y ;

    public TwoDPoint(int x,int y)//构造函数
    {
    this.x = x;
    this.y = y;
    }

    public override bool Equals(System.Object obj)
    {
    //如果参数为null,返回false。
    if(obj == null)
    {
    return false;
    }

    //如果参数不能转换为Point返回false。
    TwoDPoint p = obj as TwoDPoint;
    if((System.Object)p == null)
    {
    return false;
    }

    //如果字段匹配则返回true:
    return(x == p.x)&& (y == p.y);
    }

    public bool Equals(TwoDPoint p)
    {
    //如果参数为null返回false:
    if((object)p == null)
    {
    return false;
    }

    //如果字段匹配则返回true:
    return(x == p.x)&& (y == p.y);
    }

    public override int GetHashCode()
    {
    return x ^ y;
    }
    }

    Java有非常相似的机制。 equals()方法是 Object 类的一部分,如果你想要这种类型的功能,你的类重载它。



    重载==的原因对于对象来说可能是一个坏主意,通常,你仍然希望能够做是这些相同的指针比较。这些通常依赖于,例如,将一个元素插入到一个不允许重复的列表中,如果这个操作符以非标准的方式重载,你的一些框架的东西可能无法工作。


    I came across this recently, up until now I have been happily overriding the equality operator (==) and/or Equals method in order to see if two references types actually contained the same data (i.e. two different instances that look the same).

    I have been using this even more since I have been getting more in to automated testing (comparing reference/expected data against that returned).

    While looking over some of the coding standards guidelines in MSDN I came across an article that advises against it. Now I understand why the article is saying this (because they are not the same instance) but it does not answer the question:

    1. What is the best way to compare two reference types?
    2. Should we implement IComparable? (I have also seen mention that this should be reserved for value types only).
    3. Is there some interface I don't know about?
    4. Should we just roll our own?!

    Many Thanks ^_^

    Update

    Looks like I had mis-read some of the documentation (it's been a long day) and overriding Equals may be the way to go..

    If you are implementing reference types, you should consider overriding the Equals method on a reference type if your type looks like a base type such as a Point, String, BigNumber, and so on. Most reference types should not overload the equality operator, even if they override Equals. However, if you are implementing a reference type that is intended to have value semantics, such as a complex number type, you should override the equality operator.

    解决方案

    It looks like you're coding in C#, which has a method called Equals that your class should implement, should you want to compare two objects using some other metric than "are these two pointers (because object handles are just that, pointers) to the same memory address?".

    I grabbed some sample code from here:

    class TwoDPoint : System.Object
    {
        public readonly int x, y;
    
        public TwoDPoint(int x, int y)  //constructor
        {
            this.x = x;
            this.y = y;
        }
    
        public override bool Equals(System.Object obj)
        {
            // If parameter is null return false.
            if (obj == null)
            {
                return false;
            }
    
            // If parameter cannot be cast to Point return false.
            TwoDPoint p = obj as TwoDPoint;
            if ((System.Object)p == null)
            {
                return false;
            }
    
            // Return true if the fields match:
            return (x == p.x) && (y == p.y);
        }
    
        public bool Equals(TwoDPoint p)
        {
            // If parameter is null return false:
            if ((object)p == null)
            {
                return false;
            }
    
            // Return true if the fields match:
            return (x == p.x) && (y == p.y);
        }
    
        public override int GetHashCode()
        {
            return x ^ y;
        }
    }
    

    Java has very similar mechanisms. The equals() method is part of the Object class, and your class overloads it if you want this type of functionality.

    The reason overloading '==' can be a bad idea for objects is that, usually, you still want to be able to do the "are these the same pointer" comparisons. These are usually relied upon for, for instance, inserting an element into a list where no duplicates are allowed, and some of your framework stuff may not work if this operator is overloaded in a non-standard way.

    这篇关于什么是“最佳实践”用于比较引用类型的两个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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