C# 如何构建包含列表的新对象<;字符串>;作为属性?

C# 如何构建包含列表的新对象<;字符串>;作为属性?,c#,C#,因此,在我的主表单类中,我有以下列表,由用户输入填充: List<string> ProduseAlese = new List<string>(); 我的班级是这样的: public class Comenzi { public int NrComanda { get; set; } public DateTime DataComanda { get; set; } public DateTime DataLivrare { get; set;

因此,在我的主
表单
类中,我有以下列表,由用户输入填充:

List<string> ProduseAlese = new List<string>();
我的班级是这样的:

public class Comenzi
{
    public int NrComanda { get; set; }
    public DateTime DataComanda { get; set; }
    public DateTime DataLivrare { get; set; }
    public List<string> Articole { get; set; }
    public Facturi Factura { get; set; }

    public Comenzi(int nrcomanda, DateTime datacomanda, DateTime datalivrare, Facturi _factura, List<string> _articole)
    {
        NrComanda = nrcomanda;
        DataComanda = datacomanda;
        DataLivrare = datalivrare;
        Factura = _factura;
    }
}
公共类科门齐
{
公共int NrComanda{get;set;}
公共日期时间数据通信指令{get;set;}
公共日期时间数据集{get;set;}
公共列表文章{get;set;}
公共事实事实事实{get;set;}
公共社区(国际社区、日期时间数据社区、日期时间数据社区、事实社区、列表社区)
{
NrComanda=NrComanda;
DataComanda=DataComanda;
datalivrener=datalivrener;
事实=_事实;
}
}
我必须在构造函数中写什么?
关键是将项目从初始列表复制到新创建的对象中

这应该有效:

public List<string> Articole { get; set; } = new List<string>();
public Facturi Factura { get; set; }

public Comenzi(int nrcomanda, DateTime datacomanda, DateTime datalivrare, Facturi _factura, List<string> _articole)
{
    NrComanda = nrcomanda;
    DataComanda = datacomanda;
    DataLivrare = datalivrare;
    Factura = _factura;
    _articole = _articole?? new List<string>();
    Articole.AddRange(_articole.ToArray());
}
public List article{get;set;}=new List();
公共事实事实事实{get;set;}
公共社区(国际社区、日期时间数据社区、日期时间数据社区、事实社区、列表社区)
{
NrComanda=NrComanda;
DataComanda=DataComanda;
datalivrener=datalivrener;
事实=_事实;
_articole=_articole??新列表();
article.AddRange(_article.ToArray());
}
这应该可以:

public List<string> Articole { get; set; } = new List<string>();
public Facturi Factura { get; set; }

public Comenzi(int nrcomanda, DateTime datacomanda, DateTime datalivrare, Facturi _factura, List<string> _articole)
{
    NrComanda = nrcomanda;
    DataComanda = datacomanda;
    DataLivrare = datalivrare;
    Factura = _factura;
    _articole = _articole?? new List<string>();
    Articole.AddRange(_articole.ToArray());
}
public List article{get;set;}=new List();
公共事实事实事实{get;set;}
公共社区(国际社区、日期时间数据社区、日期时间数据社区、事实社区、列表社区)
{
NrComanda=NrComanda;
DataComanda=DataComanda;
datalivrener=datalivrener;
事实=_事实;
_articole=_articole??新列表();
article.AddRange(_article.ToArray());
}

如果要将列表对象复制到新列表中,可以使用
System.Linq
命名空间中的
ToList()
方法

Articole = _articole.ToList();

如果要将列表对象复制到新列表中,可以使用
System.Linq
命名空间中的
ToList()
方法

Articole = _articole.ToList();
也许您不必“必须”使用类构造函数来提供所有对象数据,除非有需要这样做的需求

因为您的属性是公共的,所以您可以在创建对象后为它们指定一个值

Comenzi comanda = new Comenzi(nrCom, dataCom, dataLiv, factura);
comanda.Articole = ProduseAlese;
请记住,在开发应用程序时,类定义可能会增长,填充构造函数中的所有属性不是一个好的做法。您必须只保留该类的对象所必需的数据。

也许您不必“必须”使用类构造函数来提供所有对象数据,除非有需要这样做

因为您的属性是公共的,所以您可以在创建对象后为它们指定一个值

Comenzi comanda = new Comenzi(nrCom, dataCom, dataLiv, factura);
comanda.Articole = ProduseAlese;

请记住,在开发应用程序时,类定义可能会增长,填充构造函数中的所有属性不是一个好的做法。您必须只保留该类对象的必需项。

您可能只需要
article=\u article。除非您想创建列表的单独副本,否则您可以使用类似于
article=newlist(_article)的东西
article=_article.ToList()。非常感谢!我不知道为什么我认为列表更复杂。你可能只是想要
article=\u article。除非您想创建列表的单独副本,否则您可以使用类似于
article=newlist(_article)的东西
article=_article.ToList()。非常感谢!我不知道为什么我认为列表更复杂。这将引发空引用异常。在调用AddRange之前,您不需要创建新的Articole。不,不会。请参阅第一行。这将引发空引用异常。在调用AddRange之前,您不需要创建新的Articole。不,不会。请参阅第一行。如果您仍然需要一个新列表而不是对原始列表的引用,可以使用comanda.article=ProduseAlese.ToList();如果您仍然需要一个新列表而不是对原始列表的引用,可以使用comanda.Articole=ProduseAlese.ToList();