C#的list的常用的方法,List的粗略记录,将一个list的元素给另外一个list的元素赋值

List的极其简单的学习记录

在写东西的时候如果数据很多,就会使用list来存储数据,需要使用的时候就需要将数据取出来然后进行操作。接下来就介绍一些list的简单操作以及一些常用或者不常用的方法。
LIst的微软文档

list的声明

List<string> str_list=new List<stirng>();

上面声明了一个string类型的list,这个list中只能存储string类型的数据。
string类型为值类型。(值类型的微软C#文档)。

如果创建一个引用类型的list呢?
那就要知道什么是引用类型,然后才能创建出引用类型的list(引用类型)。然后我们创建出一个引用类型的list

static void Main()
{
		List<Student> student_List=new List<Student>();
}


 class Student
 {
		public string Name;
		public string Sex;
 }

一些简单的方法

介绍一些我常用的方法,防止以后抽抽还知道怎么使用。
多创建几个对象让后放到list中去,后面就可以使用了。

    class Program
    {
        static void Main(string[] args)
        {
            //第一组list
            List<Student> student_List = new List<Student>();
            Student stu1 = new Student() { Name = "学生一", Sex = "Nan" };
            Student stu2 = new Student() { Name = "学生二", Sex = "Nv" };
            Student stu3 = new Student() { Name = "学生三", Sex = "Nan" };
            Student stu4 = new Student() { Name = "学生四", Sex = "NV" };
            Student stu5 = new Student() { Name = "学生五", Sex = "Nan" };
            student_List.Add(stu1);
            student_List.Add(stu2);
            student_List.Add(stu3);
            student_List.Add(stu4);
            student_List.Add(stu5);

            //第二组list
            Student stu_10 = new Student() { Name = "学生四", Sex = "NV" };
            Student stu_11 = new Student() { Name = "学生五", Sex = "Nan" };
            List<Student> student_List_2 = new List<Student>();
            student_List_2.Add(stu_10);
            student_List_2.Add(stu_11);

        }
    }

    class Student
    {
        public string Name;
        public string Sex;
    }

添加Add

使用上面的代码创建的list,Add的微软文档

Student stdd=new Student(){Name="goodCooking",Sex="Nan"};//创建对应的对象,将对象添加到对象类型的list中去
student_List.Add(stdd);

插入某一个元素 Insert

将元素插入 List 的指定索引处。
文档链接

Student stdd=new Student(){Name="goodCooking",Sex="Nan"};
student_List.Insert(2);//在将stdd插入到索引2

两list相加 AddRange

文档链接

            student_List.AddRange(student_List_2);

            //再测试一下是深复制还是浅复制
            //修改student_List_2中的值对相加之后student_List是否有影响
            student_List_2[0].Name = "我被修改了啊";
            Console.WriteLine(student_List[5].Name);
            Console.ReadLine();

            //输出:我被修改了啊 

            //所以是浅复制,

删除 Remove

文档链接

            student_List.Remove(student_List[0]);//移除第一个

清空 Clear

文档链接

            student_List.Clear();
			//list会被清空,因为是引用类型,在使用AddRange之后还是可以访问student_List_2的。

查找某一元素 FitstOfDefult

文档链接

            var b = student_List.FirstOrDefault(p => p.Name == "学生一");//使用了拉姆达表达式
            Console.WriteLine(b.Name);
            Console.Read();

查找某一元素 Find

文档链接,使用方式和 FitstOfDefult 差不多。

            var b = student_List.Find(p=> p.Name== "学生一");
            Console.WriteLine(b.Name);
            Console.Read();

查找某一群体元素 Where

文档链接,使用Where获得的新的list对于原本的list的数据为引用类型,所以修改where返回的list也会同时修改原本的list的数据

            //查找所有性别的为NV的然后修改为女
            //这个是区分大小写的
            var c = student_List.Where(p=>p.Sex=="NV");
            foreach (var item in c)
            {
                item.Sex = "女";
            }
            foreach (var item in student_List)
            {
                Console.WriteLine(item.Sex);
            }

            Console.Read();
		输出结果:
		Nan
		Nv
		Nan
		女
		Nan

查找某一组元素 FindAll

文档链接

       var d=     student_List.FindAll(p=>p.Sex=="NV");//获取新的list 和where一样

确认某一元素是否存在 Contains

文档链接,再次添加一个元素,没啥用,就是想加一个

            Student stu6 = new Student() { Name = "学生六", Sex = "NV" };
            student_List.Add(stu6);
            var e = student_List.Contains(student_List.Find(p => p.Name == "学生六"));//存在
            var f = student_List.Contains(student_List.Find(p => p.Name == "GoodCooing"));
            Console.WriteLine(e);
            Console.WriteLine(f);
输出结果为: 
True
False  

遍历每一个元素 ForEach

文档链接,会对每一个元素做相同的操作。

		//带有条件的 ForEach 
		        student_List.ForEach(p =>
            {
                if (p.Sex == "NV")
                {
                    p.Sex = "女";
                }
            });
				输出结果:
				Nan
				Nv
				Nan	
				女
				Nan
				女

			//不带有条件的ForEach 
            student_List.ForEach(p => p.Sex = "男");

            foreach (var item in student_List)
            {
                Console.WriteLine(item.Sex);
            }


输出结果:
男
男
男
男
男
男


有空再次补充吧。

青轴真难用,太硬了,

Enjoy

不懂评论私信,共同进步。

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值