温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

New 的含义和List<T>的数据结构

发布时间:2020-05-30 17:13:57 来源:网络 阅读:2281 作者:刺激乐天派 栏目:编程语言

在做项目数据同步开发的时候,我碰到一个很奇怪的显现。我首先从数据库获取数据集DataSet然后用Model把数据封装,然后放到List<Model>中,最后我遍历List<Model>时发现,它里面只放了最后一条数据封装的model.我把代码贴出来如下:

    /// <summary>
    /// 学生实体类
    /// </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);
            }


尝试着去打断点测试,后来发现是自己的一个想当然给搞砸的。因为只要把Student student=new Student();放在foreach里面就不会有问题了。代码如下:

foreach (DataRow row in ds.Tables[0].Rows)
            {
                Student student = new Student();//这语句解决了那个问题。
                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);
            }


正所谓知其然,也要知其所以然。现在我来分析下原因:

    我的本意呢是想尽量少的分配内存空间去创建实例(优化性能的考虑),所以想通过一个model就作为中转,可是我没有理解对List<T>的真正原理。我本以为只要我把list.add(model)list就会拷贝一份model中的信息,实际上List<T>只是添加了一个指向model的地址指针,所以每次添加都会添加一个指向model的地址指针,但他们都指向了同一个地址。所以无论你中间add了多少,(期间,list的指针都指向model地址)只是取决于最后一条数据。


    如果把Student student=new Student();放在foreach中那么每次都会去给model开辟一片内存空间,而list不在指向同一个地址,所以就不会造成数据被覆盖的假象了。


    总结:

        New 的含义就是开辟一片内存创建实例。

        List<T> 中只是保存了一个指向地址指针。

    最后我想告诉大家,数据结构很重要,有些东西很细微,如果不懂的话就很难找出其中的问题。如果我说的有什么不对的请和我分享你的看法。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI