[英]C# list or array of class
I would like to create multiple objects/instances in a loop. 我想在一个循环中创建多个对象/实例。 how to do this?
这个怎么做?
class lh
{
public string name;
public lh(string name)
{
this.name = name; ;
}
}
while((line = ps.ReadLine()) !=null)
{
lh a = new lh(line);
}
obviously I can't create a new object using the same name (a) over and over again 显然,我不能一遍又一遍地使用相同的名称(a)创建新对象
List<lh> objects = new List<lh>();
while(your_condition)
{
objects.Add(new lh(line));
}
How about a list or array? 列表或数组怎么样?
List<lh> lines = new List<lh>();
while((line = ps.ReadLine()) !=null)
{
lines.Add(new lh(line));
}
You could add your instances to a list: 您可以将实例添加到列表中:
List<lh> lhs = new List<lh>();
while ((line = ps.ReadLine()) != null)
lhs.Add(new lh(line));
var list = new List<lh>();
while((line = ps.ReadLine()) !=null)
{
list.Add(new lh(line));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.