对象初始化器和集合初始化器

 1 namespace CollectionInitializer
 2 {
 3     class Employee
 4     {
 5         /// <summary>
 6         /// Constructor.
 7         /// </summary>
 8         /// <param name="firstName"></param>
 9         /// <param name="lastName"></param>
10         public Employee(string firstName,string lastName)
11         {
12             FirstName = firstName;
13             LastName = lastName;
14         }
15 
16         /// <summary>
17         /// Achieve attribute by himself.
18         /// </summary>
19         public string FirstName { get; set; }
20 
21         public string LastName { get; set; }
22 
23         public string Salary { get; set; }
24     }
25 }
 1 using System.Collections.Generic;
 2 
 3 namespace CollectionInitializer
 4 {
 5     class Program
 6     {
 7         static void Main(string[] args)
 8         {
 9             // Object initializer.
10             var employee = new Employee("Wang", "Datong")
11                                     {
12                                         Salary = "456"
13                                     };
14 
15             // Collection initializer.
16             var list = new List<Employee>()
17                                       {
18                                           new Employee( "Employee1FirstName","Employee1LastName"),
19                                           new Employee( "Employee2FirstName","Employee2LastName"),
20                                       };
21         }
22     }
23 }

 

转载于:https://www.cnblogs.com/nogic/archive/2013/01/18/2866117.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值