Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object == equality fails, but .Equals succeeds. Does this make sense? [duplicate]

Tags:

c#

.net

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!

like image 454
sgtz Avatar asked Oct 08 '12 04:10

sgtz


2 Answers

(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.

like image 90
rizalp1 Avatar answered Oct 04 '22 10:10

rizalp1


The code sample returns true.

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

I think you have provided different example than the code returning false in your application. CLR uses string interning for string optimization. Casting to System.Object will cause == operator to compare the references and due to string interning feature, the == operator will result in true. The comparison will only return false if parameters on both sides of the == operator will refer to different string objects on heap.

Check if the assembly has been marked with [assembly: CompilationRelaxations(System.Runtime.CompilerServices.CompilationRelaxations.NoStringInterning)] attribute or not.

like image 23
jags Avatar answered Oct 04 '22 12:10

jags