0
我有一个类商店。我需要知道什么是更好的方法,以及下面的初始化有什么不同。列表初始化构造函数或属性
class Store
{
// do we initialise List of Item here in property
public List<Item> Items { get; set; } = new List<Item>();
public Store()
{
// we can instantiate list in constructor
Items = new List<Item>();
}
public Store(string myString)
{
// lets say we have another constructor here and if this one is called
// than List in default constructor will not be initialised
}
}
- 是更好地初始化列表中的财产?
- 属性初始化和构造函数有什么区别?
- 何时调用属性初始化?
- 令我惊讶的是,我没有初始化List(我注释过行),并且在创建Store类的新实例时没有抛出错误System.NullReferenceException。如果我评论List实例化,为什么它没有抛出错误。我使用VS 2015,它可以自动在这个版本。