Special case of C # Reference type string

Source: Internet
Author: User

In C # programming often use String type, it is a reference type, but everywhere is not used as a reference to use, is a special case, down I one by one listed, for their own memory convenience:

1) The direct assignment of the string: The string itself is a reference type and should use an instance of the new object method, but Microsoft can directly define string variables and assign operations for convenience, for example: string a = "my Chinese Heart"; , which simply simplifies our operations;

2) One string is assigned to another string variable: the normal reference type points two reference variables to the same address, but when a string variable is assigned to another string variable, two different address spaces are established when missing, for example:

String a = "12345"; String B = A;

The above code is two different address references, only the string content of a is assigned to B,a and B content is the same;

3) Multiple assignments of the same string: according to the general thinking of a string variable assignment, just change its contents, will not change its address, but the string is more wonderful, when the same string variable is assigned again, it will reallocate memory space, create a new address, and then the

This address assigns a value to the original string variable, for example:

String a= "123"; A = "456"

When the second assigns a value of "456", it creates a new memory space and assigns the new memory address to the a variable, and the previous "123" memory is discarded and waits for garbage collection.

4) The string is passed as a function parameter: when the string is passed as a function parameter, itself is a reference type, it should be passed the address reference of the variable, and subsequent modifications to the parameter in the function will change the value of the string, but I tell you, the result is simply passing a copy of the string to

function body, in the function of the character modification, actually does not affect the value of the passed parameter, of course, the string can also be used when the reference type, mainly add ref, you can:

5) string comparison: when a string is referenced, compare two reference types for equality, just compare two referenced addresses for equality (unless you overload the equal function), but when we compare strings, we find that they compare the contents of strings. is not a referenced address,

This is a reference to the string class overloaded with the equal function, refers to the content of the comparison character, at this point = = and equal The result is actually the same;

6) Memory-resident of a string: when we create a variable with the same string content, these string variables actually point to the same memory address, which is a bit like inline in C + +;

If there are other special, please master to give guidance, if there are any differences of opinion, leave a message, we learn from each other, rookie can grow up step by step, hehe!!!

Special case of C # Reference type string

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.