How can we combine c# accessor declaration and initialization
List<string> listofcountries= new List<string>();
and
List<string>listofcountries {get;set;}
Is there a way to combine these to statements ?
You can't at the moment. You will be able to in C# 6:
List<string> Countries { get; set; } = new List<string>();
You can even make it a read-only property in C# 6 (hooray!):
List<string> Countries { get; } = new List<string>();
In C# 5, you can either use a non-automatically-implemented property:
// Obviously you can make this read/write if you want
private readonly List<string> countries = new List<string>();
public List<string> Countries { get { return countries; } }
... or initialize it in the constructor:
public List<string> Countries { get; set; }
public Foo()
{
Countries = new List<string>();
}
See more on this question at Stackoverflow