对象==等于失败,但.Equals成功。这是否有意义? [英] Object == equality fails, but .Equals succeeds. Does this make sense?

查看:248
本文介绍了对象==等于失败,但.Equals成功。这是否有意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

可能重复:
  <一href="http://stackoverflow.com/questions/9529422/difference-between-operator-and-equals-method-in-c">Difference与在C#中==操作符和equals()方法?

两种形式平等的,第一失败,第二个成功

 (对象)ABC==(对象)ABC
假

((对象)ABC)。等于((对象)ABC)
真正
 

在下面的参考,我们看到了平等在C#中运营商不是多态

REF:字符串相等的==操作符在C#

BTW:还是不知道为什么这是一个问题。我想这可能是这样,但不是因为这个测试成功。

 静态无效的主要(字串[] args){
        变种O =新classOfT&LT;字符串&GT;(){VAL =ABC};
        Console.WriteLine((对象)o.val ==ABC);
    }

    公共类classOfT&LT; T&GT; {
        公共字符串VAL {获得;组; }
    }
 

BTW:我不与精确复制裁定不服关于这个问题,但嘿。

答案说:

  

... String类包含一个静态布尔等于(字符串,   字符串二)方法和静态布尔等于(对象A,对象B)的方法。   所不同的是前者是String类中定义   本身,而后者是从Object类继承(这是   字符串的基类)

语义这是有道理的,但它是有意义的面向对象的更广泛的范围和C#语言?

为什么我困扰的问题吗?那么,就发现了一个bug,我想下一个没头没脑的,而不是在我的大脑文件这个只记得这个,它咬你之前类

更新:

目前在思考这个与原语工作方面(从功能的角度)VS多态性。既然我已经做了越来越多的功能性的东西,这可能是为什么母语搞糊涂了。我没有做过考虑这个,但(没有我不务实。我吸引到语言设计)。感谢您的答复大家!

解决方案

 (对象)ABC
 

将建立从字符串对象的对象引用。这样算下来

 (对象)ABC==(对象)ABC
 

将创建两个对象引用,这是不相等的。

然而,使用equals方法将检查是否存储字符串的值是相等的。再次,这是不是所有的对象的默认实现,但String对象。对于任何自定义对象,你应该定义你自己的自定义实现equals方法来实现这种行为。

Possible Duplicate:
Difference between == operator and Equals() method in C#?

Two forms of equality, the first fails, the second succeeds.

(object)"abc" == (object)"abc"
false

((object)"abc").Equals((object)"abc")
true

In the below reference, we see "Equality operators in C# are not polymorphic"

ref: String Equality operator == in c#

btw: still not sure why this is an issue. I thought it might be something like this, but it isn't because this test succeeds.

    static void Main(string[] args) {
        var o = new classOfT<string>() { val = "abc" };
        Console.WriteLine((object)o.val == "abc");
    }

    public class classOfT<T> {
        public string val { get; set; }
    }

BTW: I DO NOT AGREE WITH THE EXACT DUPLICATE RULING ON THIS QUESTION, BUT HEY.

The Answer states:

... The String class contains both a static bool Equals(string a, string b) method and a static bool Equals(object a, object b) method. The difference is that the former is defined within the String class itself, whilst the latter is inherited from the Object class (which is the base class of String)

Semantically this makes sense, but does it make sense in the wider context of OO and the C# language?

Why am I bothering with the question? Well, just found a bug, and I'd like to file this in my brain under a rhyme or reason rather than under the "just remember this", it's bitten you before category.

Update:

currently thinking about this in terms of working with primitives (from a functional perspective) Vs polymorphism. Since I've been doing more and more functional stuff, this is probably why the mother tongue confused me. I'm not done thinking about this yet (no I'm not being pragmatic. I am drawn to language design). Thanks for responding all!

解决方案

(object)"abc" 

will create an Object reference from the string object. So doing

(object)"abc" == (object)"abc"

will create two object references, which are not equal.

However, using the equals method will check to see if the value of the string stored is equal. Again, this is not the default implementation of all objects, but of the String object. For any custom object, you should define your own custom implementation of equals method to achieve this behavior.

这篇关于对象==等于失败,但.Equals成功。这是否有意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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