In depth understanding of string

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

This paper just personal summary views, do not spray

First, be sure the string is a reference type

string s_a = "yhc";

string s_b = s_a;

if(s_a.Equals(s_b))

Console.WriteLine("The same?");

else

Console.WriteLine("Not the same");


The output is “ ”, s_b=s_a, is essential to the s_b to YHC on the heap storage location, the s_a and s_b to YHC on the heap storage location.

In theory if you modify the value of s_a, such as s_a=” wq”, B will also change, but actually not, in c#, if you modify one of the value of a string object such as s_a, s_a system will re apply for a block of memory to store the modified s_a value WQ, and B still point to the a memory location today.


This is why in if the frequent time for the string operation is proposed using StringBuilder, Because StringBuilder is similar to c++ in the string standard library, Can dynamically increase the memory size of the, But the c# string is a fixed size, Will assign you a fixed size memory space you string an object, You cannot change the object once created, If you want to add., I had to create a new string object, The value is copied., Then put before the string object deletion, The frequent operation of lower string will lead to low efficiency, After all, create object, Allocate memory, The process of deleting the object needs to be time-consuming and resource.

We can compare the memory address to confirm this conclusion (ReferenceEquals static method is used to compare the object reference is consistent, rather than simple values), if s_a and s_b pointing to the same address, return the same comparison should be here

s_a = "wq";

if (ReferenceEquals(s_a,s_b))

Console.WriteLine("The same");

else

Console.WriteLine("Not the same");

The answer is obvious, Will output “ not the same”, This shows that the system is s_a redistribution of new address memory space, Of course, you might say, why not==, Because the string = = to be rewritten, In fact, comparison or string value, (Of course, if you must use = = is possible, Just need to pack into object type, Let = = no longer as compared to override the string case, If(((object)s_a)==( (object)s_b))), But the = = when compared to other objects can not., This is the string reference types show a bit of a value type characteristics, In fact, there is still a place also showed a value of type string type features, In the string type as the parameter passing, Transfer the string object references., But to modify the string object inside transfer methods of the past values without affecting the body, This is because the system according to the transmitted address re structure allocates a new string object, For example, a call to the method void fun (string a); and the s_a is passed as a parameter., The a system will be re assigned an address, Instead of pointing to the s_a address, This is a reference to an exception types. For example, A is an ordinary class, for example

A b=new A();

b.name=”yhc”;

fun(b);

//The code to run here, if the output, the output value of name for WQ: a

void fun(A a)

{

//The code to run here, if the output, the output value of name for YHC: a

a.name=”wq”;//Because the parameter is a B reference, the a operation is the operation of B

//The code to run here, if the output, the output value of name for WQ: a

}

But if this is:

A b=new A();

b.name=”yhc”;

fun(b);

//The code to run here, if the output, the output value of Name: a YHC

void fun(A a)

{

//The code to run here, if the output, the output value of name for YHC: a

a=new A();

a.name=”wq”;

//The code to run here, if the output, the output value of name for WQ: a

}

Fun (b) play in the implementation of the output, It is no longer WQ, Because in the method of fun internal, We point a to a new building on the heap object A, the new object is GC garbage collection in leave function method, When the recovery is not know, Of course, if it is built on the stack resource will be immediately released, But GC reclaims the object on the establishment of the heap. There are exceptions, Is the unmanaged objects, Like what SqlConnection database connection, the file handle, network connection., These objects in the method of internal NEW, Leave the method will not be GC recovery.


The above said run far., We returned to the, Of course, some people will say that ReferenceEquals is not deep enough and accuracy, Because s_a to WQ, Values have changed, Compared with ReferenceEquals can not judge the value or reference is different in different, So we have the following modifications: according to the modified string system will automatically for the principle of the allocation of new memory space we modify s_a to YHC, Although modified, But still maintain the original YHC value, In order to test the modified operation there is no re distribution of memory space

s_a = "yhc";

if (ReferenceEquals(s_a,s_b))

Console.WriteLine("The same");

else

Console.WriteLine("Not the same");

We all thought that the output will be “ not the same”, But, But let us down, The system output “ the same”, The reason is Microsoft CLR using optimization techniques, Called string interning Technology, The technique we probably all know, When the CLR initialization will create an internal hash table, Table is the key form, The key to a string, Here YHC, Value of YHC in the heap memory storage., When the initialization list will be empty., JIT compiler (JIT) compile time to find the string YHC hash table, The first could not find YHC, It will open up space for the YHC reactor, Then put the address and the YHC string stored in the hash table, Then the second went to the, Here is the change the value of s_a to YHC, YHC found the value found in the hash table key, So instead of allocating memory to store the modified YHC, But s_a still points to the YHC before the heap storage space, This that is to say, Your revised s_a=“yhc”, Although the revised, But its value does not change, Or YHC, It is the compiler optimization mechanism optimization., So the test or tests not out we will effect.


The principle of testing defects above we know, , but we almost forgot what is what to test, we want to test whether s_a or s_b after modification, system for its re allocation of memory address space, we have to compare the modified two is consistent, then summarize the above lessons as follows:

s_a=string.Copy(s_b)

if (ReferenceEquals(s_a,s_b))

Console.WriteLine("The same");

else

Console.WriteLine("Not the same");


Here, s_a=string.Copy (s_b), the string.Copy method is to create a System.String with the same value with the specified s_b instance. Attention is a new example, with this method, the compiler will not do string interning optimization techniques above, then the s_a was again modified, modify the value is copied the s_b values (s_b = YHC), then the s_a value is YHC, then the system output “ not the same ”., this validation of our string object is modified, the system will re allocates memory space for him.


Complete test code as follows:

The test is very simple, but to many

string s_a = "yhc";

string s_b = s_a;

s_a = string.Copy(s_b);

if (ReferenceEquals(s_a,s_b))

Console.WriteLine("The same");

else

Console.WriteLine("Not the same");

Console.Read();

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Ignatz at November 13, 2013 - 8:42 AM