新学c#麻烦指点下
有2个类
第一个:
class example
{
private int id;
private string name;
public void goods(int id,string name)
{
this.id=id;
this.name=name;
}
}
如何在第二个类里面用数组的方式添加id 和name
创建数组 example[] a=null ;
后面应该怎么写?
c#
class
------解决方案-------------------- class example
{
public int id;
private string name;
public void goods(int id, string name)
{
this.id = id;
this.name = name;
}
}
class test
{
public void msd()
{
example[] a = new example[5];
for (int i = 0; i < 5; i++)
{
a[i] = new example();
a[i].goods(i, i.ToString());
}
foreach (example exaa in a)
{
Console.WriteLine(exaa.id.ToString());
}
}
}
楼主是这个意思么?
------解决方案--------------------class text
{
example[] a=new example[3];
public void()
{
for(int i=0;i<3;i++)
{
a[i]=new example();
a[i].goods(i,"name"+i.tostring());
}
foreach(example m in a)
{
console.writeline(m.id.tostring()+m.name.tostring());
}
}
}