黄聪:用ForEach方法对List<T>中对每一个项目调用同一个方法

对一个List<T>中每一个对象都进行一个函数操作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Person graham
= new Person("Graham", "Hill");
Person emerson
= new Person("Emerson", "Fittipaldi");

List
<Person> Persons = new List<Person>() { graham, emerson };

//对Persons中每个对象都执行Action方法
Persons.ForEach(Action);
}

static void Action(Person p)
{
Console.WriteLine(p.FirstName
+ " " + p.LastName);
}
}

public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }

public Person(string first, string last)
{
FirstName
= first;
LastName
= last;
}
}
}

原文地址:https://www.cnblogs.com/huangcong/p/1734458.html