美文网首页
对象的容器--集合

对象的容器--集合

作者: lianzhanshu | 来源:发表于2017-05-29 20:36 被阅读0次
    • 新建控制台应用程序ListDemo
    • 新建Student类
    namespace ListDemo
    {
        /// <summary>
        /// 学生类
        /// </summary>
        class Student
        {
            public int StudentId { get; set; }
            public string StudentName { get; set; }
            public int Age { get; set; }
            public string PhoneNumber { get; set; }
            public string StuAddress { get; set; }
        }
    }
    
    • 创建容器集合
    namespace ListDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<Student> stuList = new List<Student>();//创建泛型集合
                Student objStu1 = new Student() { StudentId = 1001, StudentName = "小王", Age = 20 };
                Student objStu2 = new Student() { StudentId = 1001, StudentName = "小张", Age = 20 };
                Student objStu3 = new Student() { StudentId = 1001, StudentName = "小刘", Age = 20 };
                Student objStu4 = new Student() { StudentId = 1001, StudentName = "小李", Age = 20 };
                Student objStu5 = new Student() { StudentId = 1001, StudentName = "小朱", Age = 20 };
                //添加对象到容器
                stuList.Add(objStu1);
                stuList.Add(objStu2);
                stuList.Add(objStu3);
                stuList.Add(objStu4);
                stuList.Add(objStu5);
                for (int i = 0; i < stuList.Count;i++ )
                {
                    Console.WriteLine(stuList[i].StudentName+"\t"+stuList[i].Age+"\t"+stuList[i].StudentId);
                }
    
                Console.ReadKey();
            }
        }
    }
    
    • 运行效果

    相关文章

      网友评论

          本文标题:对象的容器--集合

          本文链接:https://www.haomeiwen.com/subject/camqfxtx.html