VS 2008 概述 2--对象、集合初始化器

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)}

            };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值