我正在尝试在C#中向MailAddress添加多个字符串.
如果我要使用ForEach,我的代码看起来像
foreach (var item in GetPeopleList())
{
m.Bcc.Add(new MailAddress(item.EmailAddress));
}
Run Code Online (Sandbox Code Playgroud)
我现在正试图用我的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;
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了几种版本/变体,但我显然做错了.我阅读了Eric Lippert关于它的页面,遗憾的是这也无济于事.
你需要类似的东西
people.ForEach(Person p => {
m.Bcc.Add(new MailAddress(p.EmailAddress));
});
Run Code Online (Sandbox Code Playgroud)
ForEach您可以ForEach在列表中添加单个项目人员,而不是添加选定的单个项目范围.
那就是说...我自己更喜欢常规foreach循环.
| 归档时间: |
|
| 查看次数: |
288 次 |
| 最近记录: |