I have this simple code:
public static void Main(String[] args) { Data data = new Data { List = { "1", "2", "3", "4" } }; foreach (var str in data.List) Console.WriteLine(str); Console.ReadLine(); } public class Data { private List<String> _List = new List<String>(); public List<String> List { get { return _List; } } public Data() { } }
So, when I create the Data class:
Data data = new Data { List = { "1", "2", "3", "4" } };
The list was filled with the lines "1", "2", "3", "4", even if it did not have set .
Why is this happening?
c # properties getter-setter object-initializers collection-initializer
Asbrand
source share