使用List<>在一个foreach里面

时间:2012-02-09 19:03:13

标签: c#

我创建了一个名为people的列表,其中包含标识号,名称,标题和城市。我想使用foreach自动读出这个列表,但是我对语法有点困惑。有人可以对此有所了解。以下是我的代码。

List<Person> people = new List<Person>();

Person p1 = new Person(74, "Brad","Millington","Program Manager", "Milford");
Person p2 = new Person(58, "John", "Kaufman", "Author", "Ottawa");
Person p3 = new Person(68, "-*", "Washington" , "Developer", "Redmond");
Person p4 = new Person(79, "Abraham", "Licoln", "Developer", "Redmond");

foreach (string s in people)
{
    people.Add(s);
}

6 个答案:

答案 0 :(得分:6)

您的人员列表在开头是空的

您可能希望将p1,p2,p3和p4添加到人员列表

Person p1 = new Person(74, 'Brad', 'Millington', 'Program Manager', 'Milford');
people.add(p1);

然后在你的每一个中,如果你打印出来我假设你有一个名字的吸气剂

foreach (Person p in people) 
{
    Console.Write(p.getFirstName() + ' ' + p.getLastName());
}

我自己没有做过任何C#,但这应该基于我的Java经验

答案 1 :(得分:4)

作为对其他正确答案的补充,我注意到创建测试数据的更好方法是使用集合初始值设定项

List<Person> people = new List<Person>() 
{
    new Person(74, "Brad","Millington","Program Manager", "Milford"),
    new Person(58, "John", "Kaufman", "Author", "Ottawa"),
    new Person(68, "-*", "Washington" , "Developer", "Redmond"),
    new Person(79, "Abraham", "Licoln", "Developer", "Redmond")
};

foreach (Person person in people)
{
    Console.WriteLine(person.Name);
}

答案 2 :(得分:3)

foreach语句用于遍历包含数据的枚举,而不是首先填充集合。你不能在没有物品的情况下循环 - 在你的情况下,你正试图为每个物品做一个&#34;&#34;&#34;在人们中#34;但是人们是空的,因为你还没有添加任何东西。

如果您尝试将4个人添加到列表中,则需要直接添加:

List<Person> people = new List<Person>();

people.Add(new Person(74, "Brad","Millington","Program Manager", "Milford"));
people.Add(new Person(58, "John", "Kaufman", "Author", "Ottawa"));
people.Add(new Person(68, "-*", "Washington" , "Developer", "Redmond"));
people.Add(new Person(79, "Abraham", "Licoln", "Developer", "Redmond"));

(请注意,如果您愿意,也可以使用集合初始化语法...)

添加人员后,您可以循环播放,然后根据需要打印出来:

foreach(Person person in people)
    Console.WriteLine(person.FirstName + " " + person.LastName);

注意,此时,循环将循环并在每次迭代时为您提供Person实例,而不是字符串。为了在你写的时候得到foreach,你必须为Person类隐式转换为字符串(我不建议)。

答案 3 :(得分:2)

好的,您的代码存在一些问题:

  1. peoplePerson个对象的列表,因此在字符串列表中迭代它会给你一个编译错误。

  2. 使用foreach时,无法修改正在迭代的集合。您是通过在循环中使用.Add()来完成此操作。

  3. people为空,因此循环不会执行。

  4. 此代码将编译,并在控制台中写下每个人的名字和姓氏:

    List<Person> people = new List<Person>();
    
    Person p1 = new Person(74, "Brad","Millington","Program Manager", "Milford");
    Person p2 = new Person(58, "John", "Kaufman", "Author", "Ottawa");
    Person p3 = new Person(68, "-*", "Washington" , "Developer", "Redmond");
    Person p4 = new Person(79, "Abraham", "Licoln", "Developer", "Redmond");
    
    people.Add(p1);
    people.Add(p2);
    people.Add(p3);
    people.Add(p4);
    
    foreach (Person p in people)
    {
        Console.WriteLine(p.FirstName + " " + p.LastName);
    }
    

答案 4 :(得分:1)

您可以使用列表初始化程序:

var people = new List<Person>{
    new Person(74, "Brad","Millington","Program Manager", "Milford"),
    new Person(58, "John", "Kaufman", "Author", "Ottawa"),
    new Person(68, "-*", "Washington" , "Developer", "Redmond"),
    new Person(79, "Abraham", "Licoln", "Developer", "Redmond")
};

答案 5 :(得分:0)

听起来你想要填充一个列表,然后在foreach循环中读出它的值。如果这就是你想要做的,那么问题的核心是:“我的对象的字符串表示是什么?”

例如,如果您呼叫p.ToString() pPerson,则默认情况下会获取其类型名称。在创建对象时,有时候覆盖ToString()的隐含基类的Person方法很有用,即System.Object。请注意,当您覆盖ToString()方法时,如果您还不覆盖GetHashCode()Equals(),编译器会发出警告。