C # Special case of reference type string,

Source: Internet
Author: User

C # Special case of reference type string,

In C # programming, the string type is often used. It is also a reference type, but it is not used for reference everywhere. It is a special case. I will list it one by one, for your convenience:

1) direct value assignment of strings: The string itself is the reference type. You should use the new object method to create an instance. However, for convenience, Microsoft can directly define string variables and assign values. For example: string a = "My Chinese heart";, this is just to simplify our operations;

2) assign a value to another string variable:A normal reference type directs two referenced variables to the same address, but when one string variable is assigned to another string variable, two different address spaces are created when the variable is missing. For example:

String a = "12345"; string B =;

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

3) Multiple assignments of the same string:Assign a value to a string variable according to the general thinking. It only changes its content and does not change its address. However, the string is odd. When you assign a value to the same string variable again, it will re-allocate the memory space, create a new address, and then

This address is assigned to the original string variable, for example:

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

When a is assigned "456" for the second time, it creates a new memory space and assigns the new memory address to the variable, in the past, "123" of the memory was discarded and not needed, waiting for garbage collection.

4) the string is passed as a function parameter:When a string is passed as a function parameter, it is a reference type. It should be passed through the variable address reference, and the value of the string will be changed in future changes to this parameter in the function, but I tell you, the result is that it only passes a copy of the string

The function body, which is modified in the function, does not affect the value of the passed parameter. Of course, the transfer of a string can also be used when the reference type is used. You can add a ref parameter. You can:

5) string comparison:When the string is used as a reference, compare whether two reference types are Equal, only to compare whether the two referenced addresses are Equal (unless you have overloaded the Equal function), but when we compare strings, they found that they compared the string content, not the referenced address,

This references the string class to overload the equal function, which refers to the content of the comparison character. At this point, the = and equal results are actually the same;

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

If you have any other special points, please give them some advice. If you have any different opinions, please leave a message and everyone will learn from each other so that cainiao can grow up step by step !!!

Related Article

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.