Preface
The string type is the most used type in our actual project development. Everyone knows that string is a reference type, but in the actual use process, you will find that string is really different from the common reference types we use. See a simple example below:
static void Main(string[] args) { string hello = "my name is yuanHong"; (("Value before processing:{0}", hello)); ////// Process hello MachHello(hello); (("Processed value:{0}", hello)); (); } /// <summary> /// Process hello /// </summary> /// <param name="hello"></param> private static void MachHello(string hello) { hello = ("{0},Nice to meet you",hello); }
The actual result of the program running is: the values before and after are the same and have not changed. If you look at the reference type, the values before and after processing should be different. Why? Does it feel like a string is like a value type? OK Let’s discuss the particularity of string together.
Introduction to internal string implementation
First of all: It is to be explained that string is modified by sealed and cannot be inherited.
Secondly: Through the above string underlying source code, we found that in the underlying implementation, we actually use the char array to implement it. When initializing a string, the system has initialized the size of the char array.
The string is fixed in size when created, and is read-only and cannot be modified.
In actual use, our change to string actually recreates a new string internally.
When a string is passed as a function parameter, it actually copies a copy of the data.
Finally: Now when we look back at the results of the most open program, it is not difficult for us to understand why this phenomenon occurs.
Points to note when using string
1. Avoid extra storage space overhead
Avoid using the + sign to splice strings:
See an example below:
string str1 = "yuan"; str1 = str1 + "hong"; //// This will create two strings and three string objects string strNew = "yuan" + "hong";/// equivalent to strNew="yuanhong", in fact, this effect is the same after compilation//// Only one string object will be created
Let's look at another example:
//// The following are two ways to return a string 123 //// Method 1string v11="1"; string v22=v11+"2"; string v33=v22+"3"; retun v33; //// Use method 1: The system will create 5 string objects //// Method 2//// Use Method 2: The system will only create 4 string objectsstring v1="1"; string v2="2"; string v3="3"; retun v1+v2+v3; //// Who can do the memory overhead,The obvious way2Better than method1
In actual development, if you frequently splice string objects, it is recommended to use StringBuilder
Of course, there is also a simplified string splicing method in C#: In fact, its internal implementation principle is StringBuilder
2. Pack as little as possible
Directly upload the code example:
string str1 = "yunghong" + 66; string str2 = "yunghong" + (); //// View compiled code,Discover the first line of code,Need to have a packing operation,Packing operation,Need to increase unnecessary memory overhead,First:The value type needs to be distributed to the memory,At the same time, memory overhead must be allocated to type pointers and indexes of the same section.
Summarize:
Some points to note in actual development:
1. Avoid packing operations
2. Avoid using + sign splicing strings
OK, the above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support.