C # special reference type with value type characteristics of string "string has constant"

Source: Internet
Author: User
Tags true true

The string type is directly inherited from the object, so it is a newReference classType, that is, the thread stack does not reside with any strings.

(All value types are inherited from system. valuetype. It is worth noting that system. valuetype is
Reference type)

Code 1:
String str1 = "string"; string str2 = "stri" + "NG"; 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 whenDuring CLR initialization, it creates
An internal hash
,The key is a string, and the value is a reference to the string object in the managed heap..

However,Dynamically created stringInstead of querying the hash listCreate a New String object directly in the managed heap.String str3 = "system." + str1;

When str1 is constructed, the "string" string exists in the hash list,
If it does not exist, a New String object will be constructed in the managed heap, and the "string" string and reference pointing to this object will be added to the hash list, when str2 is constructed, the value is assigned to str2 because the reference key is "string" in the hash table. str1 and str2 reference the same string object, the Code returns as soon as it is generated.
True

Code 2:
Static void main (string [] ARGs) {string STR = "string ";Change(STR); console. writeline (STR );}

Static void change (string Str) {STR = "changed ";}

The parameter passed by the method isCopy of original content, The process:

Statement STR = "changed"

Statement STR = "changed"

In this way, we can see that the original string object has not changed, STR = "changed" is just to create a New String object (Another reference type is to change the value pointed to by memory address 1.), So the parameters of this method need to be addedRef or out Modifier.

It can also be obtained hereCharacter string is constantThat is to say, once a string is created, we can no longer extend it,
Or change any character.

Code 3:
String str1 = "string"; string str2 = "system." + "string"; string str3 = "system." + str1; string str4 = "system. String ";

Console. writeline (string. Equals (str2, str3); true

Console. writeline (string. referenceequals (str2, str3); false

Console. writeline (string. Equals (str4, str2); true

Console. writeline (string. referenceequals (str2, str4); true

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. This is becauseDynamically created stringInstead of querying the hash listCreate a New String object directly in the managed heap, Such as the statement stringStr3 =
"Syetem." + str1
Therefore, if you use string. referenceequals to compare str2 and str3, false is returned.
If string. referenceequals is used to compare str2 and str4, true is returned. Of course, you can manually add str3 strings to the hashed list and return
Use: str3 =
String. Intern (str3), so using string. referenceequals to compare str2 and str3 will return true. As
String. Equals returns true because string overwrites the equals method and checks whether two references point to the same object. If yes, true is returned,
If not, compare the characters.

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

@, Overcome the reading obstacle caused by escape strings.

Double. parse, tryparse (STR, out num); Convert. todouble, exception handling methods are different.

Passed by reference. The ref parameter must be initialized before being passed, and there is no requirement for out.

Sizeof obtains the value type and does not apply to reference types. The value type is inherited from the valuetype, which inherits object, but it overrides equals method, by value comparison, instead of reference address. The 'string' method is also overloaded to compare the value size.

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.