我正在尝试将多个字符串添加到 C# 中的 MailAddress 中。
如果我要使用ForEach
,我的代码看起来像
foreach (var item in GetPeopleList())
{
m.Bcc.Add(new MailAddress(item.EmailAddress));
}
我现在正试图用我的 foreach (即List.ForEach()
)来做到这一点,但我做不到。
public class Person
{
public Person(string firstName, string lastName, string emailAddress)
{
FirstName = firstName;
LastName = lastName;
EmailAddress = emailAddress;
}
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
}
static void Main(string[] args)
{
MailMessage m = new MailMessage();
List<Person> people = GetPeopleList();
m.Bcc.Add(people.ForEach(Person people =>
{
//what goes here?
}
));
}
private static List<Person> GetPeopleList()
{
List<Person> peopleList = new List<Person>();
//add each person, of type Person, to the list and instantiate the class (with the use of 'new')
peopleList.Add(new Person("Joe", "Bloggs", "Joe.Bloggs@foo.bar"));
peopleList.Add(new Person("John", "Smith", "John.Smith@foo.bar"));
peopleList.Add(new Person("Ann", "Other", "Ann.Other@foo.bar"));
return peopleList;
}
我已经尝试了几个版本/变体,但我显然做错了。我阅读了Eric Lippert 的页面,遗憾的是这也没有帮助。