Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why object.Equals and instanceobject.Equals are not same

        string s1 = "t";
        string s2 = 't'.ToString();        

        Console.WriteLine(s1.Equals(s2)); // returning true
        Console.WriteLine(object.Equals(s1, s2)); // returning true

Here it is returning same result. Now when I'm using StringBuilder it is not returning same value. What is the underneath reason?

        StringBuilder s1 = new StringBuilder();
        StringBuilder s2 = new StringBuilder();

        Console.WriteLine(s1.Equals(s2)); // returning true
        Console.WriteLine(object.Equals(s1, s2)); // returning false

Edit1: My above question answered below. But during this discussion what we find out StringBuilder doesn't have any override Equals method in its implementation. So when we call StringBuilder.Equals it actually goes to Object.Equals. So if someone calls StringBuilder.Equals and S1.Equals(S2) the result will be different.

like image 759
Pritam Karmakar Avatar asked Mar 19 '12 01:03

Pritam Karmakar


People also ask

Why are two identical objects not equal to each other?

Objects are not compared by value: two objects are not equal even if they have the same properties and values. This is true of arrays too: even if they have the same values in the same order. var a = {}; // The variable a refers to an empty object. var b = a; // Now b refers to the same object.

Why use .equals instead of == Java?

We can use == operators for reference comparison (address comparison) and . equals() method for content comparison. In simple words, == checks if both objects point to the same memory location whereas . equals() evaluates to the comparison of values in the objects.

What is the difference between equals () and ==?

equals() method. The major difference between the == operator and . equals() method is that one is an operator, and the other is the method. Both these == operators and equals() are used to compare objects to mark equality.

Is it possible for equals () to return false even if contents of two objects are same?

Yes. You can also override the equals() method and play with it.


1 Answers

String.Equals() is overriden in C# such that identical strings are in fact Equal() when the Equal() override defined on string is used.

If you are comparing string literals (not the case in your example), it's worth noting that identical string literals are interned... that is, identical strings live at the same address so will also be equal by reference (e.g. object.Equals() or s1.ReferenceEquals(s2)) as well as by value.

StringBuilder provides an overload to Equals() that takes StringBuilder as a parameter (that is s1.Equals(s2) will call that overload instead of calling object.Equals(object obj)).

http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.equals.aspx

StringBuilder.Equals() is...

true if this instance and sb have equal string, Capacity, and MaxCapacity values; otherwise, false.

object.Equals() uses the static Equals() defined on object, which checks only for reference equality (if passed a class) or for value equality (if passed a struct).

So in summary

string s1 = "t";
string s2 = 't'.ToString();        

Console.WriteLine(s1.Equals(s2)); // true because both reference equality (interned strings) and value equality (string overrides Equals())
Console.WriteLine(object.Equals(s1, s2)); // true because of reference equality (interned strings)

StringBuilder s1 = new StringBuilder();
StringBuilder s2 = new StringBuilder();

Console.WriteLine(s1.Equals(s2)); // true because StringBuilder.Equals() overloaded
Console.WriteLine(object.Equals(s1, s2)); // false because the two StringBuilder instances have different addresses (references not equal)
like image 129
Eric J. Avatar answered Sep 28 '22 09:09

Eric J.