使用列表<>;前臂内侧

本文关键字:gt 列表 lt | 更新日期: 2023-09-27 18:24:01

我创建了一个名为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);
}

使用列表<>;前臂内侧

您的人员列表在开始时当前为空

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

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

然后在你的for中,如果你要打印出来,我假设你有一个名为的getter

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

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

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

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

foreach语句用于循环遍历包含数据的可枚举对象,而不是首先填充集合。你不能在没有项目的情况下循环浏览某个东西——在你的情况下,你试图在人中为每个人做一个",但人是空的,因为你还没有添加任何内容。

如果你想把这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类隐式转换为字符串(我不建议这样做)。

好吧,您的代码有一些错误:

  1. peoplePerson对象的列表,因此将其作为字符串列表进行迭代会导致编译错误。

  2. 使用foreach时,不能修改正在迭代的集合。您是通过在循环中使用.Add()来实现这一点的。

  3. people为空,因此无论如何都不会执行循环。

这段代码将编译,并在控制台上写下每个人的名字和姓氏:

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

您可以使用列表Initializer:

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")
};

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

例如,如果您调用p.ToString(),其中pPerson,则默认情况下您将获得其类型名称。创建对象时,有时覆盖Person(即System.Object)的隐含基类的ToString()方法是有用的。请注意,当您重写ToString()方法时,如果您不同时重写GetHashCode()Equals(),编译器将向您发出警告。