2

Code Block

string c = "Hello", d = "Hello";

Console.WriteLine("c is of Type : {0}, d is of Type : {1} ", c.GetType(),d.GetType());
Console.WriteLine("c==d {0}", object.ReferenceEquals(c, d));
Console.WriteLine("c and d hashCode are equal {0}", 
c.GetHashCode() == d.GetHashCode());

Output

c is of Type : System.String, d is of Type : System.String

c==d True

c and d hashCode are equal True

Code Block

string e = "Hello", f = "Hello";

e += " world";
f += " world";

Console.WriteLine("e is of Type : {0}, f is of Type : {1} ", c.GetType(),d.GetType());
Console.WriteLine("e==f {0}", object.ReferenceEquals(e, f));
Console.WriteLine("e and d hashCode are equal {0}", e.GetHashCode() == f.GetHashCode());

Output

e is of Type : System.String, f is of Type : System.String

e==f False

e and f hashCode are equal True

Question

Two different “strings” are the same object instance?

it is mentioned the Compiler is optimized that it will automatically reference to same string, which is relevant for variable c and d.

but in the case of variable e and f it should have pointed to the same string because e and f are re-assigned with new reference when we try to do += operation on string (stings are immutable),

but as per Answer in above link the variable f should have been assigned to the reference of e, but according to output these two variables were assigned new reference. Why so?

Community
  • 1
  • 1
Shekhar Pankaj
  • 9,065
  • 3
  • 28
  • 46
  • 3
    "They should be pointing to a different address in memory" - what makes you think that? You have two constant string expressions with the same value. They're guaranteed (by the C# language specification) to refer to the same string object. – Jon Skeet May 09 '17 at 10:59
  • @JonSkeet So its save to say they are pointing at the value in the memory? Out of curiosity :) – EpicKip May 09 '17 at 12:03
  • @EpicKip, yes the C# compilers are optimized to handle it such that it will automatically handle the string creation in same assembly, if the new string matches any existing string it dont create new object rather it just assign reference to existing one to new variable. So yes they will be pointing to same value. – Shekhar Pankaj May 09 '17 at 12:12
  • @EpicKip: It's safe to say the values of the two variables are the same reference, yes. – Jon Skeet May 09 '17 at 13:17
  • @JonSkeet, Edited the original question, can you help me with answer to that? Check second code snippet and its output – Shekhar Pankaj May 09 '17 at 13:20
  • I'm afraid your question is asked very unclearly, but `e` and `f` do not refer to compile-time constants by the time you've assigned new values. I don't know what you're asking. – Jon Skeet May 09 '17 at 13:23
  • @JonSkeet if edit still make sense :| – Shekhar Pankaj May 09 '17 at 13:33
  • Not really - you're claiming "In the case of variable `e` and `f` it should have pointed to he same `string` because `e` and `f` are re-assigned with new reference when we try to do `+=` operation on `string`" - that seems like a complete non-sequitur to me. There's a difference between "compile-time constants known to have the same text" and "strings that are created at execution time and happen to have the same text". – Jon Skeet May 09 '17 at 13:50

0 Answers0