Home > Article > Backend Development > Detailed introduction to the special case string of reference types in C#
When programming in C#, the string (string) type is often used. It is also a reference type, but it is not used as a reference everywhere. It is a special case. I will list them one by one for your own convenience:
1) Direct assignment of strings: The string itself is a reference type, and an instance of the new object method should be used. However, for the convenience of everyone, Microsoft can directly define string variables and perform assignment operations. , for example: string a = "My Chinese Heart"; , this just simplifies our operations;
2) Assign a string to another string variable: Normal reference type The two reference variables will point to the same address, but when one string variable is assigned to another string variable, two different address spaces are created, for example:
string a = "12345"; string b = a;
The above code is two Different address references, just assign the string content of a to b, the contents of a and b are the same;
3) Multiple assignments of the same string: Follow the general Assigning a value to a string variable only changes its content and does not change its address. However, strings are weird. When the same string variable is assigned a value again, it will reallocate the memory space and create a new address. , and then assign the address
to the original string variable. For example:
string a= "123"; a = "456"
When a is assigned a value of "456" for the second time, it creates a new memory space. Then assign the newly created memory address to the a variable, and discard the previous "123" memory, waiting for garbage collection.
4) Passing a string as a function parameter: When a string is passed as a parameter of a function, it is a reference type. The address reference of the variable should be passed, and later in the function Modification of this parameter will change the value of the string, but let me tell you, it only passes a copy of the string to the
function body. Modifying the character in the function does not affect the transfer. The value of the parameter, of course, the transmission of the string can also be used as a reference type, the main REF can be added. :
When strings are used as references, the two reference types are compared for equality, only the addresses of the two references are compared for equality (unless you overload the Equal function), but when we compare strings, we find that in fact What they compare is the content of the string, not the reference address.This is the reference string class overloaded with the equal function, which refers to comparing the content of the characters. At this point, the results of == and equal are actually are the same;
6) Memory residency of strings: When we create variables with the same string content, these string variables actually point to the same memory address , this is a bit like inlining in C++;
If there are any other special points, please ask the experts to give advice. If you have any different opinions, please leave a message. Everyone can learn from each other, so that novices can grow up step by step. ,hehe! ! !
The above is the detailed content of Detailed introduction to the special case string of reference types in C#. For more information, please follow other related articles on the PHP Chinese website!