我有一个类商店。我需要知道什么是更好的方式,以及在初始化中有什么区别。
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
}
}
答案 0 :(得分:1)
还有类似问题Difference between (auto) properties initialization syntax in C# 6