詳解C#中使用對(duì)象或集合的初始值設(shè)定項(xiàng)初始化的操作
使用對(duì)象初始值設(shè)定項(xiàng)初始化對(duì)象
可以使用對(duì)象初始值設(shè)定項(xiàng)以聲明方式初始化類型對(duì)象,而無(wú)需顯式調(diào)用類型的構(gòu)造函數(shù)。
下面的示例演示如何將對(duì)象初始值設(shè)定項(xiàng)用于命名對(duì)象。編譯器通過先訪問默認(rèn)實(shí)例構(gòu)造函數(shù)然后處理成員初始化處理對(duì)象初始值設(shè)定項(xiàng)。因此,如果默認(rèn)構(gòu)造函數(shù)在類中聲明為 private,那么需要公共訪問權(quán)的對(duì)象初始值設(shè)定項(xiàng)將失敗。
下面的示例演示如何使用對(duì)象初始值設(shè)定項(xiàng)初始化新的 StudentName 類型。
public class Program { public static void Main() { // Declare a StudentName by using the constructor that has two parameters. StudentName student1 = new StudentName("Craig", "Playstead"); // Make the same declaration by using an object initializer and sending // arguments for the first and last names. The default constructor is // invoked in processing this declaration, not the constructor that has // two parameters. StudentName student2 = new StudentName { FirstName = "Craig", LastName = "Playstead", }; // Declare a StudentName by using an object initializer and sending // an argument for only the ID property. No corresponding constructor is // necessary. Only the default constructor is used to process object // initializers. StudentName student3 = new StudentName { ID = 183 }; // Declare a StudentName by using an object initializer and sending // arguments for all three properties. No corresponding constructor is // defined in the class. StudentName student4 = new StudentName { FirstName = "Craig", LastName = "Playstead", ID = 116 }; System.Console.WriteLine(student1.ToString()); System.Console.WriteLine(student2.ToString()); System.Console.WriteLine(student3.ToString()); System.Console.WriteLine(student4.ToString()); } }
這段的輸出是:
Craig 0 Craig 0 183 Craig 116
public class StudentName { // The default constructor has no parameters. The default constructor // is invoked in the processing of object initializers. // You can test this by changing the access modifier from public to // private. The declarations in Main that use object initializers will // fail. public StudentName() { } // The following constructor has parameters for two of the three // properties. public StudentName(string first, string last) { FirstName = first; LastName = last; } // Properties. public string FirstName { get; set; } public string LastName { get; set; } public int ID { get; set; } public override string ToString() { return FirstName + " " + ID; } }
下面的示例演示如何使用集合初始值設(shè)定項(xiàng)初始化一個(gè) StudentName 類型集合。請(qǐng)注意,集合初始值設(shè)定項(xiàng)是一系列由逗號(hào)分隔的對(duì)象初始值設(shè)定項(xiàng)。
List<StudentName> students = new List<StudentName>() { new StudentName {FirstName="Craig", LastName="Playstead", ID=116}, new StudentName {FirstName="Shu", LastName="Ito", ID=112}, new StudentName {FirstName="Gretchen", LastName="Rivas", ID=113}, new StudentName {FirstName="Rajesh", LastName="Rotti", ID=114} };
使用集合初始值設(shè)定項(xiàng)初始化字典
Dictionary<TKey, TValue> 包含鍵/值對(duì)集合。 它的 Add 方法采用兩個(gè)參數(shù),一個(gè)用于鍵,另一個(gè)用于值。 若要初始化 Dictionary<TKey, TValue> 或其 Add 方法采用多個(gè)參數(shù)的任何集合,請(qǐng)將每組參數(shù)括在大括號(hào)中,如下面的示例所示。
示例
在下面的代碼示例中,使用 StudentName 類型的實(shí)例初始化一個(gè) Dictionary<TKey, TValue>。
class StudentName { public string FirstName { get; set; } public string LastName { get; set; } public int ID { get; set; } } class CollInit { Dictionary<int, StudentName> students = new Dictionary<int, StudentName>() { { 111, new StudentName {FirstName="Sachin", LastName="Karnik", ID=211}}, { 112, new StudentName {FirstName="Dina", LastName="Salimzianova", ID=317}}, { 113, new StudentName {FirstName="Andy", LastName="Ruth", ID=198}} }; }
請(qǐng)注意集合的每個(gè)元素中的兩對(duì)大括號(hào)。 最內(nèi)層的大括號(hào)括起了 StudentName 的對(duì)象初始值,而最外層的大括號(hào)括起了將要添加到 studentsDictionary<TKey, TValue> 中的鍵/值對(duì)的初始值。 最后,字典的整個(gè)集合初始值括在一對(duì)大括號(hào)內(nèi)。
- C#使用SqlDataAdapter對(duì)象獲取數(shù)據(jù)的方法
- C#編程中使用ref和out關(guān)鍵字來傳遞數(shù)組對(duì)象的用法
- C#編程實(shí)現(xiàn)對(duì)象與JSON串互相轉(zhuǎn)換實(shí)例分析
- C#實(shí)現(xiàn)獲取不同對(duì)象中名稱相同屬性的方法
- C#中Cookie之存儲(chǔ)對(duì)象
- C#中使用反射遍歷一個(gè)對(duì)象屬性及值的小技巧
- C#查找對(duì)象在ArrayList中出現(xiàn)位置的方法
- C#檢查指定對(duì)象是否存在于ArrayList集合中的方法
- 淺談對(duì)c# 面向?qū)ο蟮睦斫?/a>
- C# 對(duì)象持久化詳解
相關(guān)文章
Unity3D實(shí)現(xiàn)人物移動(dòng)示例
這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)人物移動(dòng)示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01C#在驗(yàn)證文件共享模式下實(shí)現(xiàn)多線程文件寫入
這篇文章主要為大家詳細(xì)介紹了C#在驗(yàn)證文件共享模式下實(shí)現(xiàn)多線程文件寫入的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2024-01-01