1. C # integration is everywhere, but for beginners like mini-series, there will be some minor problems when using the set for the first time. Let's just look at the code.
Code:
1 using system; 2 using system. collections. generic; 3 using system. LINQ; 4 using system. text; 5 6 namespace cnbog20140824 7 {8 public class program 9 {10 public static void main (string [] ARGs) 11 {12 string STR = "123 "; 13 list <string> lststr = new list <string> (); 14 lststr. add (STR); 15 STR = "213"; 16 lststr. add (STR); 17 18 string [] array = new string [1]; 19 array [0] = "John "; 20 list <string []> lstarray = new list <string []> (); 21 lstarray. add (array); 22 array [0] = ""; 23 lstarray. add (array); 24 25 console. readkey (); 26} 27} 28}View code
Output result: through rapid variable monitoring, let's take a look at the content of the set. The capacity of the lststr class has changed, however, although the array content is modified after lstarray, each element of lstarray is changed to the following modified content.
Cause Analysis: all those who have learned C/C ++ know that variables are divided into value variables and address variables. The same applies to C #. For data of the string, Int, and double types, after changing the information, I applied for a new bucket. Therefore, the address and data of the variable with the same name changed, therefore, we can see that the data of lststr [0] And lststr [1] is the same; otherwise, for list <string []>, string [] and class declare a variable, in fact, the variable points to the address. Even if the address pointed to by the information variable is changed, the data is changed for the same variable, in fact, lstarray [0] And lstarray [1] point to the same data address. Of course, the data is the same.
Newcode:
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace cnbog20140824 {public class program {public static void main (string [] ARGs) {string [] array = new string [1]; array [0] = "James "; list <string []> lstarray = new list <string []> (); lstarray. add (array); array = new string [1]; array [0] = "Li Si"; lstarray. add (array); console. readkey ();}}}View code
Output result: the data in lstarray is different when the new space is used every time.
C # basic set 1