1、 对象初始化器 NET框架中的类型非常依赖于属性的使用。当生成对象实例和使用新的类型时,写出象下面这样的编码是非常常见的情形:
Person item = new Person();
item.PersonID = 1;
item.FirstName = "steven";
item.LastName = "huang";
item.Sex = 1;
item.Birthday = DateTime.Now.AddYears(-20);
items.Add(item);
使用VS2008中的C#和VB语言编译器的话,你现在可以利用一个称为“对象初始化器(object Initializers)”的“语法甜头(syntactic sugar)”语言特性来做些简化,将上述代码重写为:
Person item = new Person { PersonID = 1, FirstName = "steven", LastName = "huang", Sex = 1, Birthday = DateTime.Now.AddYears(-20) };
2 集合初始化器 对象初始化器很棒,它极大地简化了把对象添加到集合的做法。譬如,假如我要把三个人加到一个基于泛型的类型为Person的List集合中去的话,我可以写下面这样的编码:
List<Person> items = new List<Person>();
items.Add(new Person { PersonID = 1, FirstName = "steven", LastName = "huang", Sex = 1, Birthday = DateTime.Now.AddYears(-20)});
items.Add(new Person { PersonID = 2, FirstName = "brake", LastName = "chen", Sex = 0, Birthday = DateTime.Now.AddYears(-10)});
items.Add(new Person { PersonID = 3, FirstName = "scott", LastName = "huang", Sex = 1, Birthday = DateTime.Now.AddYears(-15)});
但VS2008版中的C#和VB编译器允许我们更进一步,现在同时支持“集合初始化器(collection initializers)”,这允许我们避免要写多个Add语句,省下更多的键盘操作:
List<Person> items = new List<Person>
{
new Person { PersonID = 1, FirstName = "steven", LastName = "huang", Sex = 1, Birthday = DateTime.Now.AddYears(-20)},
new Person { PersonID = 2, FirstName = "brake", LastName = "chen", Sex = 0, Birthday = DateTime.Now.AddYears(-10)},
new Person { PersonID = 3, FirstName = "scott", LastName = "huang", Sex = 1, Birthday = DateTime.Now.AddYears(-15)},
new Person { PersonID = 4, FirstName = "sum", LastName = "chen", Sex = 0, Birthday = DateTime.Now.AddYears(-5)},
new Person { PersonID = 5, FirstName = "tom", LastName = "wu", Sex = 0, Birthday = DateTime.Now.AddYears(-25)},
new Person { PersonID = 6, FirstName = "dive", LastName = "wang", Sex = 1, Birthday = DateTime.Now.AddYears(-28)},
new Person { PersonID = 7, FirstName = "sala", LastName = "zhang", Sex = 0, Birthday = DateTime.Now.AddYears(-30)},
new Person { PersonID = 8, FirstName = "joey", LastName = "Liu", Sex = 0, Birthday = DateTime.Now.AddYears(-45)},
new Person { PersonID = 16, FirstName = "alex", LastName = "huang", Sex = 1, Birthday = DateTime.Now.AddYears(-30)}
};
List<PersonNew> items = new List<PersonNew>
{
new PersonNew { PersonID = 1, FirstName = "steven", LastName = "huang", Sex = 1, Birthday = DateTime.Now.AddYears(-20)},
new PersonNew { PersonID = 2, FirstName = "brake", LastName = "chen", Sex = 0, Birthday = DateTime.Now.AddYears(-10)},
new PersonNew { PersonID = 3, FirstName = "scott", LastName = "huang", Sex = 1, Birthday = DateTime.Now.AddYears(-15)},
new PersonNew { PersonID = 4, FirstName = "sum", LastName = "chen", Sex = 0, Birthday = DateTime.Now.AddYears(-5)},
new PersonNew { PersonID = 5, FirstName = "tom", LastName = "wu", Sex = 0, Birthday = DateTime.Now.AddYears(-25)},
new PersonNew { PersonID = 6, FirstName = "dive", LastName = "wang", Sex = 1, Birthday = DateTime.Now.AddYears(-28)},
new PersonNew { PersonID = 7, FirstName = "sala", LastName = "zhang", Sex = 0, Birthday = DateTime.Now.AddYears(-30)},
new PersonNew { PersonID = 8, FirstName = "joey", LastName = "Liu", Sex = 0, Birthday = DateTime.Now.AddYears(-45)},
new PersonNew { PersonID = 9, FirstName = "joan", LastName = "wu", Sex = 0, Birthday = DateTime.Now.AddYears(-22)},
new PersonNew { PersonID = 10, FirstName = "roy", LastName = "huang", Sex = 1, Birthday = DateTime.Now.AddYears(-65)},
new PersonNew { PersonID = 15, FirstName = "canmy", LastName = "chen", Sex = 1, Birthday = DateTime.Now.AddYears(-55)},
new PersonNew { PersonID = 16, FirstName = "alex", LastName = "huang", Sex = 1, Birthday = DateTime.Now.AddYears(-30)}
};