public class Oec2003Info { string name; int age; List<string> phoneNumbers = new List<string>();
public string Name { get { return name; } set { name = value; } } public string Age { get { return age; } set { age = value; } } public List<string> PhoneNumbers { get { return phoneNumbers; } } }
以前的做法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
var oec2003infos = new List<Oec2003Info>();
var tmp1= new Oec2003Info(); tmp1.Name = "oec2003"; tmp1.Age=100; tmp1.PhoneNumbers.Add(" tmp1.PhoneNumbers.Add("1592********"); Oec2003Info.Add(tmp1);
var tmp2 = new Oec2003Info(); tmp2 .Name = "oec2003"; tmp2.Age=100; tmp2.PhoneNumbers.Add("1593********"); tmp3.PhoneNumbers.Add("1594********"); Oec2003Info.Add(tmp2);
使用对象初始化器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
var oec2003infos= new List<Oec2003Info> { new Oec2003Info { Name = "oec2003", Age=100; PhoneNumbers={"1591********","1592**********"}
},
new Oec2003Info { Name = "oec2003", Age=100; PhoneNumbers = { "1593********","1594*********"}