I have a class Store. I need to know what is better way and what is difference in below initialisation.
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
}
}
- Is it better to initialise List in property?
- What is difference between initialisation in property and in constructor?
- When property initialisation is called?
- On my surprise, I did not initialise List (I commented lines) and it did not throw an error System.NullReferenceException when I created new instance of Store class. Why it did not throw an error if I commented List instantiation. I use VS 2015, could it be automatically in this version.