Is system. String an immutable string?

Source: Internet
Author: User
When learning. NET programming, you must have heard that the system. string class is an immutable string, that is, you cannot modify the value of a string.

For exampleCode

String S =   " Hello " ;

S =   " World " ;

Instead of changing the value of S to world, you generate a new string containing "world", and then reference the string "S" to a new string, the original string is discarded.

It is based on the unmodifiable string class that CLR uses the string interning technology to share the same string to reduce memory usage. For example, the following code

String S1 =   " Hello " ;

String S2 =   " Hello " ;


If (Object. referenceequals (S1, S2 ))

Console. writeline ( " They are same " );

S1 and S2 actually point to the same string.

So is there really no way to change the value of a string? Of course not. In C ++/CLI, we can use some special means to achieve our goal!

String ^ S1 =   " Hello " ;

String ^ S2 =   " Hello " ;


Interior_ptr < Char > P = Const_cast < Interior_ptr < Char >   > (Ptrtostringchars (S1 ));


For (; * P; * P ++ = ' A ' );


Console: writeline ( " {0} and {1} " , S1, S2 );

Ptrtostringchars (string ^) is a definition in vcclr. h: A helper function in the header file. It returns an internal pointer of the interior_ptr <const char> type, pointing to the string contained in the string instance, the reason why the return type is interior_ptr <const char> instead of interior_ptr <char> is that we do not want to change the string in the string instance, but since we insist on doing so, so let's use a const_cast <t> to remove this const!

Run the above Code and you will find that the content of S2 is AAAAA, Not hello.

Of course, the above example only describes the possibility of modifying the content of the string instance, and does not encourage everyone to do so. Although direct access to strings inside the string instance may bring some performance benefits. However, because of the existence of string interning, modifying the string inside the string instance is quite risky. For example, in the above example, you will find that the output of S1 is also aaaaa!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.