简体   繁体   中英

Assigning a string-literal to a string in C#

Do string-literals have a particular type in C# (like const char* in c++) or does C# just create a new string object for each string-literal that appears in a program ? I am curious to understand what happens behind the scene when the following statement is executed:

string s1 = "OldValue";

does this call a particular method in the string class (a constructor, or an impicit conversion operator, ...) or does C# create a new string object that contains "OldValue" ( then just assign its reference to s1, just like it would for any reference type ) ?

i am trying to understand what it is in the design of the string class that garantees the value of s1 remains "OldValue":

string s2 = s1;
s2 = "NewValue";

To you last question, why values were preserved - it is not in the String class. It is in the way that object references work.

The String class is not a value type , it is a reference type . It is a full-featured object that is not copied-around when "passed intto/from variables".

When you write:

string s1 = "mom";
string s2 = s1;
string s3 = s1;
s3 = "dad";

there is only one instance of "mom", that is first created somewhere in the heap, then a reference to it is assigned to s1. Then another reference is created and assigned to s2. Then another reference is created and assigned to s3. No copies. References. Like for any real, normal CLR object .

Finally, in the last line, another string is created on the heap and then a reference to it is assigned to the s3 variable. Note that this sentence says absoltely nothing about the "mom" or s1/s2 variables. They didn't note a thing.

Remember that String is not a value-type. It is just an normal immutable object that has some handy Equals and GetHashCode overrides. String class has some little magic inside, but it is not relevant here.

Good question.

Actually in c# strings are stored in the format of buffer array where in every string declaration required 20bytes to store data and post that 2 bytes for each character. so whenever you declare any string for eg string s1 = 'Bhushan'; then on string buffer will be created and will have memory requirements as follows,

Bytes required for Data (Overhead) : 20 Bytes 2 bytes per character so (2 * 7) : 14 Bytes Overall it will required 20 + 14 = 34 Bytes.

string is an immutable class, that means every time we change the value, it will create new instance.

string s2 = s1;
s2 = "NewValue";

it can be explain like this.

string s2 = s1;
s2 = new string("NewValue"); // It doesn't compile, just an example.

And for string modification, it can be explained like this.

string s = "blah";
s.Insert(0, "blah"); // s is a new instance

The same like:

string s = "blah";
s = new string("blah") + new string("blah"); // Doesn't compile, just an explanation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM