Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference object comparison of type string

Consider the following code:

public static void Main()
{
    string str1 = "abc";
    string str2 = "abc";

    if (str1 == str2)
    {
        Console.WriteLine("True");
    }
    else
    {
        Console.WriteLine("False");
    }

    Console.ReadLine();
}

The output is "True". string is a reference type in .Net & I am comparing two different objects, but still the output is "True".

  1. Is is because it internally calls ToString() method on both objects & before comparing them?
  2. Or is it because a string is an immutable type? Two completely distinct string objects having the same value would point to same memory location on the heap?

How does string comparison happens?

How does memory allocation works on the heap? Will two different string objects with the same value point to same memory location, or to a different one?

like image 575
Atul Sureka Avatar asked Feb 21 '26 01:02

Atul Sureka


2 Answers

  • Strings are compared by value by default.
  • Objects are compared by reference by default.
  • Identical string literals in the same assembly are interned to be the same reference.
  • Identical strings that are not literals can legally be interned to the same reference, but in practice typically are not.

So now you should be able to understand why you get the given output in this program fragment:

string a1 = "a";
string a2 = "a";
string aa1 = a1 + a2;
string aa2 = a1 + a2;
object o1 = a1;
object o2 = a2;
object o3 = aa1;
object o4 = aa2;
Console.WriteLine(a1 == a2);   // True
Console.WriteLine(aa1 == aa2); // True
Console.WriteLine(o1 == o2);   // True
Console.WriteLine(o3 == o4);   // False

Does that make sense?

like image 53
Eric Lippert Avatar answered Feb 23 '26 15:02

Eric Lippert


For the string type, == compares the values of the strings.

See http://msdn.microsoft.com/en-us/library/53k8ybth.aspx

Regarding your question about addressing, a few lines of code says they will have the same address.

static void Main(string[] args)
{
    String s1 = "hello";
    String s2 = "hello";
    String s3 = s2.Clone() as String;

    Console.Out.WriteLine(Get(s1));
    Console.Out.WriteLine(Get(s2));
    Console.Out.WriteLine(Get(s3));

    s1 = Console.In.ReadLine();
    s1 = Console.In.ReadLine();
    s3 = s2.Clone() as String;

    Console.Out.WriteLine(Get(s1));
    Console.Out.WriteLine(Get(s2));
    Console.Out.WriteLine(Get(s3));
}

public static string Get(object a)
{
    GCHandle handle = GCHandle.Alloc(a, GCHandleType.Pinned);
    IntPtr pointer = GCHandle.ToIntPtr(handle);
    handle.Free();
    return "0x" + pointer.ToString("X");
}

Results in the same address for each set of tests.

Get() courtosey of Memory address of an object in C#

like image 29
Babak Naffas Avatar answered Feb 23 '26 15:02

Babak Naffas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!