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 }