二、List集合赋值示例

List集合简单赋值

//添加list集合并赋值,int类型
List<int> intlist = new List<int>();
intlist.Add(1);
intlist.Add(3);
intlist.Add(4);
foreach (var item in intlist)
{
    Console.WriteLine(item);
}

//添加list集合并赋值,string类型
List<string> stringlist = new List<string>();
stringlist.Add("张三");
stringlist.Add("李四");
stringlist.Add("王五");
foreach (var item in stringlist)
{
    Console.WriteLine(item);
}

为实体类赋值并将数据存放到集合list中

using System;
using System.Collections.Generic;

namespace demo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Student> ls = new List<Student>();
            ls.Add(new Student { ID = 1, Name = "张三" });
            ls.Add(new Student { ID = 2, Name = "李四" });
            ls.Add(new Student { ID = 3, Name = "王五" });
            foreach (var item in ls)
            {
                Console.WriteLine(item.ID + ":" + item.Name);
            }
        }
    }

    class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

}

© 版权声明
THE END
喜欢就支持一下吧
点赞11赞赏 分享
评论 抢沙发

请登录后发表评论