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.
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.
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.
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.
Yes. You can also override the equals() method and play with it.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With