【发布时间】:2017-02-02 14:16:30
【问题描述】:
新来的,我已经学习 c# 大约一个月了。
不管怎样,我已经搜索 StackOverflow 几天了,但找不到我的问题的具体答案...
//Here's my Class
public class Guy
{
public static int ID { get; set; }
public static int LifeExpectancy { get; set; }
public static bool Living { get; set; }
public Guy(int id, int lifeExpectancy, bool living)
{
ID = id;
LifeExpectancy = lifeExpectancy;
Living = living;
}
}
我要做的是创建特定数量的“someGuy”对象,然后使用这种方法将它们放入公共列表中......
public static List<Guy> Guys = new List<Guy>();
public static void makeSomeGuys(int howManyGuys)
{
for (int i = 0, i <= howManyGuys; i++)
{
int id = i;
int lifeExpectancy = 80;
bool alive = true;
Guys.Add(New Guy(id, lifeExpectancy, alive));
Console.WriteLine("Made a new Guy {0}", id);
}
return;
}
问题按重要性排序:
如何访问特定对象及其参数? (从“Guys”列表访问。)
如何在另一个类中访问此列表中的对象? (我不是绝对需要,我很好奇)
我可以使用参数在列表中搜索对象吗? (而不是像...humanPopulation[number])
我应该为修改了参数的对象创建一个新列表吗? (而不是将其留在原始列表中)
是否可以从列表中删除项目? (一般来说,这是人们做的事情吗?如果是,为什么?)
我真的只需要回答第一个问题。其余的只是奖金。谢谢!
【问题讨论】:
-
您阅读过
List的API 文档吗?这是一个链接:msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx