C#初始化列表属性

C#初始化列表属性,c#,list,properties,C#,List,Properties,我有以下课程…如何使用一些值初始化 我的问题是,如何使用Main中的一些值初始化上面的RootObject 比如说 Rootobject robj = new Rootobject(); robj.inchistor.Add() using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Js

我有以下课程…如何使用一些值初始化
我的问题是,如何使用Main中的一些值初始化上面的RootObject 比如说

    Rootobject robj = new Rootobject();
    robj.inchistor.Add()     



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace JsonSample3
    {

        public class Customerinfo
        {
            public string customername { get; set; }
            public string address { get; set; }
            public string postcode { get; set; }
        }

        public class Inchistory
        {

            public Customerinfo customerinfo { get; set; }
            public string r { get; set; }
            public string reference { get; set; }
            public string region { get; set; }

        }

        public class RootObject
        {
            public List<Inchistory> inchistory { get; set; }
        }


    }

    class Program
    {
            static void Main(string[] args)
            {
               RootObject robj = new RootObject{ r = "", }

            }
    }



   Am having above classes namely CustomerInfo, Inchistory and Rootobject
Rootobject robj=newrootobject();
robj.inchistor.Add()
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间JsonSample3
{
公共类Customerinfo
{
公共字符串customername{get;set;}
公共字符串地址{get;set;}
公共字符串邮政编码{get;set;}
}
公共级历史
{
公共Customerinfo Customerinfo{get;set;}
公共字符串r{get;set;}
公共字符串引用{get;set;}
公共字符串区域{get;set;}
}
公共类根对象
{
历史记录中的公共列表{get;set;}
}
}
班级计划
{
静态void Main(字符串[]参数)
{
RootObject robj=新的RootObject{r=”“,}
}
}
我有上面的类,即CustomerInfo、InHistory和Rootobject

任何引用类型的默认值都是
null
。因此,我假设您在尝试添加值时得到了
NullReferenceException
。可以在对象的构造函数中将list属性初始化为空列表:

public class RootObject
{
    public List<Inchistory> inchistory { get; set; }

    public RootObject()
    {
        inchistory = new List<Inchistory>();
    }
}

不知道你在问什么。您正在寻找对象和集合初始化的组合吗

RootObject robj = new RootObject() 
{
    inchistory = new List<Inchistory>() 
    {
       new Inchistory() 
       {
           r = "foo",
           reference = "bar",
           customerinfo = new CustomerInfo()
           {
                customername = "joe blogs",
                address = "somewhere",
                postcode = "xxx xxxx"
           },
           region = "somewhere"
       },
       new Inchistory()
       {
           // etc
       }
    }
};
RootObject robj=newrootobject()
{
inchistory=新列表()
{
新纪元
{
r=“foo”,
reference=“bar”,
customerinfo=新customerinfo()
{
customername=“joe博客”,
address=“某处”,
邮政编码=“xxx xxxx”
},
region=“某处”
},
新纪元
{
//等
}
}
};
或者,如果您现在没有要添加的内容,您可以执行以下操作:

RootObject robj = new RootObject() 
{
    inchistory = new List<Inchistory>() 
};
RootObject robj=newrootobject()
{
inchistory=新列表()
};

或者,您也可以按照David的建议在类构造函数中初始化列表。

如果可能,集合属性不应该有任何setter,这将使此集合不可能为null。NET序列化仍然可以工作,因为它使用AddRange(IEnumerable)而不是new List(IEnumerable)。
RootObject robj = new RootObject() 
{
    inchistory = new List<Inchistory>() 
};