带List的类<t>如何添加项目

时间:2016-09-02 13:15:42

标签: c# list

我有2个类Record和Pos。类Record有一个List。

public class Record
{
    public string RecordNr { get; set; }
    public List<Pos> Positions { get; set; }
    public bool barred { get; set; }

    public Vorgang()
    {
        RecordNr = "";
        Positions = null;
        barred = false;
    }
}

public class Pos
{
    public string PosNr { get; set; }
    public string ArtNr { get; set; }

    public Pos()
    {
        PosNr = "";
        ArtNr = "";
    }
}

如何在Record类中向List添加项目。

Pos cPos = new Pos 
{
   ArtNr = "35454",
   PosNr = "1",
};

Record cRec = new Record ();
cRec.Positions.Add(cPos);

为什么我会收到System.NullReferenceException?

0 个答案:

没有答案