Reputation: 448
Out of curiosity, if I call:
string txt = "text";
Will it call this, behind the scenes?
string txt = new String("text".ToCharArray())?
Upvotes: 1
Views: 120
Reputation: 754575
No it will not. This code will translate directly into an IL stloc
command. It will essentially compile to the following
ldstr "text"
stloc.0
Upvotes: 2
Reputation: 217263
string txt1 = "text";
loads the string "text"
from the intern pool and stores it as reference in the txt1
variable.
So, for example, if you have
string txt2 = "text";
string txt3 = "text";
then ReferenceEquals(txt2, txt3) == true
, because both variables reference the same string object in the intern pool.
The String Constructor creates a new, non-interned string object.
string txt4 = new String("text".ToCharArray());
So ReferenceEquals(txt1, txt4) == false
.
There is one exception: new String(new char[0])
returns the reference to the ""
string object in the intern pool.
Upvotes: 7
Reputation: 50110
i doubt it - why would it
But why not look for yourself, run ildasm and see what code is generated
Upvotes: -1