如何给构造函数有参数的 实体类 建实体数组?
例如一个实体类
public class DM_GBK
{
public DM_GBK(bool dm)
{
_dm = dm;
}
#region Model
private bool _dm;
private string _mc;
private string _version;
public bool DM
{
set { _dm = value; }
get { return _dm; }
}
public string MC
{
set { _mc = value; }
get { return _mc; }
}
public string VERSION
{
set { _version = value; }
get { return _version; }
}
#endregion Model
DM_GBK dm = new DM_GBK(true); 这样单个建可以
如果建立实体类数组怎么建立?
DM_GBK[] dm = new DM_GBK(true) [5]; 这么建好像不行,,
------解决方案--------------------DM_GBK[] dm = Enumerable.Range(0, 5).Select(x => new DM_GBK(true)).ToArray();
------解决方案--------------------不怕麻烦也可以写
DM_GBK[] dm = new DM_GBK[] { new DM_GBK(true), new DM_GBK(true), new DM_GBK(true), new DM_GBK(true), new DM_GBK(true) };
------解决方案--------------------C# code
DM_GBK[] dm =new DM_GBK[c];
for(int i=0;i<c;i ++)
{
dm[i]=new DM_GBK();
}
------解决方案--------------------
泛型也行的吧,如:
C# code
List<DM_GBK> dm_dbk = new List<DM_GBK>();
for (int i = 0; i < 5; i++)
{
dm_dbk.Add(new DM_GBK(true));
}
dm_dbk[0].MC = "abc";
Console.WriteLine(dm_dbk[0].MC);
Console.ReadLine();