Meaning of new and data structure of list <t>

Source: Internet
Author: User

During project data synchronization development, I encountered a strange appearance. I first retrieve the dataset from the database, encapsulate the data with the model, and put it in the list <model>. Finally, I found that when traversing the list <model>, it only contains the model of the last data encapsulation. I pasted the Code as follows:

/// <Summary> /// Student Entity class /// </Summary> class student {Public String stuno {Get; set;} public string name {Get; set ;} public String sex {Get; set;} public int age {Get; set;} Public String phone {Get; set;} Public String ADDR {Get; set ;} public datetime register_time {Get; Set ;}}
 Student student = new Student();            foreach (DataRow row in ds.Tables[0].Rows)            {                student.Stuno = row["Stuno"].ToString();                student.Name = row["Name"].ToString();                student.Sex = row["Sex"].ToString();                student.Age = Convert.ToInt32(row["Age"].ToString());                student.Phone = row["Phone"].ToString();                student.Addr = row["Addr"].ToString();                student.Register_Time = Convert.ToDateTime(row["Register_Time"]);                list.Add(student);            }            IEnumerator<Student> IEStu = list.GetEnumerator();            Student s = new Student();            while(IEStu.MoveNext())            {                s=IEStu.Current as Student;                Console.WriteLine("[{0}] [{1}] [{2}] [{3}] [{4}] [{5}] [{6}]", s.Stuno, s.Name, s.Sex, s.Age, s.Phone, s.Addr, s.Register_Time);            }


I tried to perform a breakpoint test, and later I found that it was a mistake. As long as student = new student (); is put in foreach, there will be no problem. The Code is as follows:

Foreach (datarow row in DS. Tables [0]. Rows) {student = new student (); // This statement solves that problem. Student. stuno = row ["stuno"]. tostring (); student. name = row ["name"]. tostring (); student. sex = row ["sex"]. tostring (); student. age = convert. toint32 (row ["Age"]. tostring (); student. phone = row ["phone"]. tostring (); student. ADDR = row ["ADDR"]. tostring (); student. register_time = convert. todatetime (row ["register_time"]); list. add (student);} ienumerator <student> iestu = List. getenumerator (); student s = new student (); While (iestu. movenext () {S = iestu. current as student; console. writeline ("[{0}] [{1}] [{2}] [{3}] [{4}] [{5}] [{6}]", s. stuno, S. name, S. sex, S. age, S. phone, S. ADDR, S. register_time );}


The so-called know its own, also want to know its own. Now let me analyze the cause:

My intention is to allocate as little memory as possible to create instances (optimizing performance), so I want to use a model as a transit, however, I do not understand the true principle of list <t>. I thought that as long as I put list. add (model) List copies the information in the model. In fact, list <t> only adds an address pointer to the model, therefore, each time you add an address pointer to the model, they all point to the same address. Therefore, no matter how much is added in the middle, (during this period, the list Pointer Points to the model address) is only dependent on the last piece of data.


If student = new student (); is placed in foreach, a memory space will be opened for the model each time, and the list does not point to the same address, therefore, the data will not be overwritten.


Summary:

New means opening up a piece of memory to create an instance.

List <t> only saves a pointer to the address.

Finally, I would like to tell you that the data structure is very important, and some things are very subtle. If you don't understand them, it will be difficult to find out the problems. If there is anything wrong with what I said, please share your opinion with me.

This article is from the coffee blog, please be sure to keep this source http://4837471.blog.51cto.com/4827471/1564686

Meaning of new and data structure of list <t>

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.