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();