【发布时间】:2013-10-06 19:39:27
【问题描述】:
我已经搜索了好几个小时,但似乎找不到任何答案。我试图让foreach 循环在我的对象内部使用一种方法。希望您能理解并帮助我。
Warrior.cs:
public void Info()
{
Console.WriteLine("N: "
+ this.name
+ ", L: "
+ this.level
+ ", H: "
+ this.health
+ ", D: "
+ this.damage
+ ", A: "
+ this.agility);
}
Program.cs:
List<Warrior> lista = new List<Warrior>();
for (int i = 0; i < 10; i++)
{
lista.Add(new Warrior("Swordman" + i.ToString()));
}
foreach (Warrior item in lista)
{
lista.Info();//<----------This is where I get the error
}
【问题讨论】:
-
你遇到了什么错误?
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。