集合初始化器
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ALTest.Employee 8 { 9 class Employee2 10 { 11 12 public Employee2(string firstName, string lastName) 13 { 14 FirstName = firstName; 15 LastName = lastName; 16 } 17 18 19 public string FirstName { get; private set; } 20 public string LastName { get; private set; } 21 public string Salary { get; set; } 22 public string Title { get; set; } 23 24 } 25 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 8 9 namespace ALTest.Employee 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 List<Employee2> employees = new List<Employee2>() 16 { 17 new Employee2("in","love"), 18 new Employee2("out","love") 19 }; 20 21 22 foreach (Employee2 employee2 in employees) 23 { 24 Console.WriteLine("My name :{0} {1}", employee2.FirstName, employee2.LastName); 25 } 26 27 28 Console.ReadKey(); 29 } 30 } 31 }