C # string features
When talking about strings, we naturally think it is simple, but there are always some small problems that are vague. Next I will talk about the problem of strings in the system, and some will not be supplemented in the future. 1. First, String is a class. string is only an alias of the String class. It means another code, which is used like String. 2, string str = "abc", which is different from char [] cha = {'A', 'B', 'C. Do not simply recognize that a string is a character array. Understand the essence. It is the immutability of strings. strings are only readable but not writable. The character array is readable and writable. Readable: str [0] = a str [1] = B str [2] = c, str [0] = a str [1] = B str [2] = c, writable: the elements in the string are unwritable 123456789101112131415 <span style = "font-size: 12px; font-family: ;"> class Program {static void Main (string [] args) {char [] cha = {'A', 'B', 'C'}; Console. writeLine (cha [0]); cha [0] = 'B'; Console. writeLine (cha); string str = "abc"; Console. writeLine (str [0]); // str [0] = 'D'; Console. readKey () ;}</span> If str [0] = 'd '; "Error 1 cannot assign a value to the attribute or indexer" string. this [int] "-- it is a read-only" character array. Someone suggested that str = "abc"; str + = "d", Console. WriteLine (str); abcd appears. Doesn't this indicate that the character transfer can be changed? Writable? Next, let's analyze this issue. The following figure shows the immutability of strings. Another figure proves this immutability. 3. Here again proves the immutability of the string. Another question is raised, that is, the concept of "temporary storage pool" of the string. String str = "abc"; string str1 = "abc" indicates the same object. But this is only string str = "abc"; string str1 = "abc, rather than char [] cha = {'A', 'B', 'C '}; string str = new string (cha); When debugging, open the monitoring window and enter * variable, the address of the monitoring variable. The two are the same address, which proves to point to the same variable. Different addresses naturally have different variables. To sum up, the processing string must accept its return value, because each time it is processed, an object is generated. The string instance is often ToCharArray (); and then the string (char []) constructor is called. "Modify" the elements in the string. Question: here we will think of the usage of value transfer and reference transfer. It will be updated later. 4, string str = null; str = ""; string str1 = string. Empty; string str2 = ""; string str3 = ""; // there is a space