一. 初始化器的簡單使用
using System; namespace 初始化器 { class Program { static void Main(string[] args) { //1. 常見的初始化方式,將類實例化,並通過構造函數將參數傳遞進類中,此方式通過 public StudentName(string first, string last)生效 var student1 = new StudentName("Meimei", "Wang"); //2. 沒有小括號,直接通過大括號調用類的屬性,並將賦值,此即為對象的初始化器,此方式通過 public StudentName() { } var student2 = new StudentName { FirstName = "Meimei", LastName = "Wang" }; //3. 初始化器也就是默認的構造函數,因而可以用來對任意公共屬性進行操作,而指定的構造函數指定按照類中指定的結構進行操作 var student3 = new StudentName { ID = 5 }; //student3 = new StudentName(100) 這是錯誤的 //4. 另外構造函數和初始化器可以一起使用 var student4 = new StudentName("Lei", "Li") { ID = 122 }; Console.WriteLine(student1.ToString()); Console.WriteLine(student2.ToString()); Console.WriteLine(student3.ToString()); Console.WriteLine(student4.ToString()); Console.ReadKey(); } } public class StudentName { public string FirstName { get; set; } public string LastName { get; set; } public int ID { get; set; } public StudentName() { } //初始化器,其實也就是默認的構造函數 public StudentName(string first, string last) //該類構造函數 { FirstName = first; LastName = last; } public override string ToString() { return FirstName + " " + ID; } } }
二、匿名類的初始化器的使用
using System; using System.Collections.Generic; using System.Linq; namespace 初始化器 { class Program { static void Main(string[] args) { var pet = new { Age = 10, Name = "Miaomiao" }; //匿名類中的屬性都是只讀的 //pet.Name = 100; 此處編譯會出錯 //匿名類初始化器常用語LINQ語句中 var students = new List<StudentName> { new StudentName("Li", "LI"), new StudentName("Mei", "MEI") }; var studentsFrom = new List<StudentFrom> { new StudentFrom { FirstName = "Li", City = "Beijing" }, new StudentFrom { FirstName = "Wang", City = "Shanghai" } }; var joinQuery = from s in students join f in studentsFrom on s.FirstName equals f.FirstName select new { FirstName = s.FirstName, LastName = s.LastName, City = f.City }; foreach(var i in joinQuery) { Console.WriteLine("{0} {1} {2}", i.FirstName, i.LastName, i.City); } Console.ReadKey(); } } public class StudentName { public string FirstName { get; set; } public string LastName { get; set; } public int ID { get; set; } public StudentName() { } //初始化器,其實也就是默認的構造函數 public StudentName(string first, string last) //該類構造函數 { FirstName = first; LastName = last; } public override string ToString() { return FirstName + " " + ID; } } public class StudentFrom { public string FirstName { get; set; } public string City { get; set; } } }
三、集合類初始化器
using System; using System.Collections.Generic; using System.Linq; namespace 初始化器 { class Program { static void Main(string[] args) { //集合的初始化器 CollectionInitializer(); Console.ReadKey(); } private static void CollectionInitializer() { var students = new List<StudentName> { new StudentName { FirstName = "Mei", LastName = "MEI", ID = 100 }, //使用對象的初始化器創建 new StudentName() { FirstName = "Lei", LastName = "LEI", ID = 101 }, //使用默認的構造函數+初始化器 new StudentName("Li", "LI") { ID = 102 }, //使用指定的構造函數+初始化器 null }; foreach(var i in students) { if (i != null) { Console.WriteLine(i.ToString()); } } Dictionary<int, StudentName> studentDic = new Dictionary<int, StudentName> { { 111, new StudentName { FirstName = "Mei", LastName = "MEI", ID = 100 } }, { 112, new StudentName() { FirstName = "Lei", LastName = "LEI", ID = 101 } }, { 113, new StudentName("Li", "LI") { ID = 102 } } }; foreach(var s in studentDic) { Console.WriteLine(s.Value.ToString()); } } } public class StudentName { public string FirstName { get; set; } public string LastName { get; set; } public int ID { get; set; } public StudentName() { } //初始化器,其實也就是默認的構造函數 public StudentName(string first, string last) //該類構造函數 { FirstName = first; LastName = last; } public override string ToString() { return FirstName + " " + ID; } } }
