Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"stringDemo" versus new string("stringDemo".ToCharArray);

Tags:

c#

.net

oop

c#-4.0

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?

like image 242
Masoud Darvishian Avatar asked May 03 '13 09:05

Masoud Darvishian


2 Answers

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 ==:

  • If both operands have a compile-time type of ==, the overload provided by string will be used, which performs a content comparison
  • Otherwise, the "default" implementation which just compares references for equality will be used, as you stated in your question.

In your case, the compile-time types of the operands are both object, so it is genuinely using reference equality.

like image 175
Jon Skeet Avatar answered Sep 23 '22 21:09

Jon Skeet


Perhaps you are confused because use are using the types object instead of string.

If you compare two objects 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 strings 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.
like image 3
Martin Mulder Avatar answered Sep 23 '22 21:09

Martin Mulder