please look at the below code:
using System;
class MyClass
{
static void Main()
{
object o = ".NET Framework";
object o1 = new string(".NET Framework".ToCharArray());
Console.WriteLine(o == o1);
Console.WriteLine(o.Equals(o1));
}
}
and the result is:
False
True
and now consider this one:
using System;
class MyClass
{
static void Main()
{
object o = ".NET Framework";
object o1 = ".NET Framework";
Console.WriteLine(o == o1);
Console.WriteLine(o.Equals(o1));
}
}
and the result is:
True
True
“==” compares if the object references are same while “.Equals()” compares if the contents are same. and i want to know what is different between these codes?!
object o1 = new string(".NET Framework".ToCharArray());
and
object o1 = ".NET Framework";
both of them turn out an object but why results are different?
both of them turn out an object but why results are different?
In the second case, you're using the same string constant for both the o
and o1
assignment. C# guarantees that any two equal string constant expressions within the same program will refer to the same string object. So the values of o
and o1
are the same reference.
While I can't find the more general form (for constant string expressions), your case is actually covered by section 2.4.4 of the C# spec:
When two or more string literals that are equivalent according to the string equality operator appear in the same program, these string literals refere to the same string instance.
EDIT: A quick note on the behaviour of ==
:
==
, the overload provided by string
will be used, which performs a content comparisonIn your case, the compile-time types of the operands are both object
, so it is genuinely using reference equality.
Perhaps you are confused because use are using the types object
instead of string
.
If you compare two object
s via the operator ==
, only the references to those objects are compared. And since two constant strings inside the same assembly are merge as one, they have the same reference.
If you would compare two string
s via the operation ==
than enother method is used. String has an operator override for ==
. See: http://msdn.microsoft.com/en-us/library/system.string.op_equality(v=vs.110).aspx. This override does not compare the reference, it compares the value of both objects. In your example, the compiler cannot now both types are of type string
because you are using objects
. That is why the string operation ==
is not used to compare o
and o1
.
Back to the Equals
function. Equals
is a function which can be overridden by inheriting classes. In this case, the string
class has overridden it and replace it with his own method of comparison. Where object.Equals
compares only references, string.Equals
compares values.
EDIT
So... this will produre your "strange" values:
object o = ".NET Framework";
object o1 = new string(".NET Framework".ToCharArray());
Console.WriteLine(o == o1); // Will prodcuce: False.
Console.WriteLine(o.Equals(o1)); // Will prodcuce: True.
And this will produce expected values:
string o = ".NET Framework";
string o1 = new string(".NET Framework".ToCharArray());
Console.WriteLine(o == o1); // Will prodcuce: True.
Console.WriteLine(o.Equals(o1)); // Will prodcuce: True.
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