C # string details _ reprint

Source: Internet
Author: User
Tags true true

. NET FrameworkProgramThe design (revision) has the following description: The string type directly inherits from the object, which makes it a reference type. That is to say, the stack on the thread does not reside with any strings. Note that "direct inheritance" here ". Directly inherited from the object type must be the reference type, because all value types inherit from system. valuetype. It is worth noting that system. valuetype is a reference type .).

CodeI:
StringStr1 ="String";

StringStr2 ="String";

Console. Writeline (String. Referenceequals (str1, str2 ));

Since the string type is a reference type, the output of code 1 should be false, but in fact the output of code 1 is true. This is because when CLR is initialized, it creates an internal hash. The key is a string, and the value is a reference to the string object in the managed heap. When str1 is constructed, the "string" string exists in the hash list. If the string does not exist, a New String object is constructed in the managed heap, then, add the "string" string and the reference pointing to this object to the hashed list. When str2 is constructed, the hashed list contains a reference with key as "string, so the value is assigned to str2, so str1 and str2 reference the same string object, and the code returns true as soon as it does.

 

Code 2:
Static voidMain (String[] ARGs)

{

StringSTR ="Old string";

Console. Writeline (STR); change (STR );

Console. Writeline (STR );

}

Static voidChange (StringStr)

{

STR ="Changed string";

}

The parameter passed by the method is the copy of the original content, and the process can be expressed:

Output before statement STR = "changed string"

Old string

Output after the statement STR = "changed string"

Old string

In this way, we can see that the original string object does not change STR = "changed" but only creates a new String object (other reference types are to change the value pointed to by memory address 1 ), therefore, the ref or out modifier must be added to the parameters of this method. Therefore, we can also conclude that a string has a constant, that is, once a string is created, we can no longer extend, shorten, or change any character in it.

 

Code 3:
StringStr1 ="String";

StringStr2 ="System ."+"String";

StringStr3 ="System ."+ Str1;

Console. Writeline (String. Equals (str3, str2 ));

Console. Writeline (String. Referenceequals (str2, str3 ));

StringStr4 ="System. String";

Console. Writeline (String. Equals (str4, str2 ));

Console. Writeline (String. Referenceequals (str2, str4 ));

Based on the analysis of code 1 and 2, the output result of code 3 is: True true, but this is not the case. The correct result is: True False True true. This is because the dynamically created string does not query the hash list, but directly creates a new String object in the managed heap, such as the statement string str3 = "syetem. "+ str1, so use string. when referenceequals is used to compare str2 and str3, false is returned, whereas string. if referenceequals is used to compare str2 and str4, true is returned. Of course, you can manually add the str3 string to the hash list and return the reference: str3 = string. intern (str3), which uses string. if referenceequals is used to compare str2 and str3, true is returned. For string. the reason why equals returns true is that string overrides the equals method. Internally, it checks whether the two references point to the same object. If true is returned, it then compares the characters.

 

From: http://www.diybl.com/course/4_webprogram/asp.net/netjs/20091107/181566.html

 

Code 4:

String S1 = "a string ";

String S2 = S1;

Console. writeline ("S1 is" + S1 );

Console. writeline ("S2 is" + S2 );

S1 = "another string ";

Console. writeline ("S1 is now" + S1 );

Console. writeline ("S2 is now" + S2 );

Output result:

S1 is a string

S2 is a string

S1 is now another string

S2 is a string

When S1 is initialized with the value "a string", a string object is allocated in the heap. When S2 is initialized, the reference also points to this object, so S2 is also "a string ". But when S1 is changed, instead of returning the original value, the heap will allocate a new object for the new value, and S2 will still point to the original object.

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.