C# 批量操作对象属性

class Test
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
            List<Test> list = new List<Test>();
            list.Add(new Test { Name = "张三", Age = 12 });
            list.Add(new Test { Name = "李四", Age = 26 });
            list.Add(new Test { Name = "老王", Age = 18 });

            //方式一
            Console.WriteLine("****************************方式一****************************");
            foreach (Test item in list)
            {
                item.Age = 11;
                Console.WriteLine(item.Name + ":" + item.Age);
            }
            //方式二
            Console.WriteLine("****************************方式二****************************");
            list.All(o => { o.Age = 22; return true; });
            foreach (Test item in list)
            {
                Console.WriteLine(item.Name + ":" + item.Age);
            }
            //方式三
            Console.WriteLine("****************************方式三****************************");
            list.ForEach(o => o.Age = 33);
            foreach (Test item in list)
            {
                Console.WriteLine(item.Name + ":" + item.Age);
            }

            Console.ReadLine();            

C# 批量操作对象属性

 

 

  

上一篇:泛型(学习补充)


下一篇:Java8 Stream(三):StreamAPI