C # basic set 1

Source: Internet
Author: User

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

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.