使用對象初始值設定項初始化對象
可以使用對象初始值設定項以聲明方式初始化類型對象,而無需顯式調用類型的構造函數。
下面的示例演示如何將對象初始值設定項用於命名對象。編譯器通過先訪問默認實例構造函數然後處理成員初始化處理對象初始值設定項。因此,如果默認構造函數在類中聲明為 private,那麼需要公共訪問權的對象初始值設定項將失敗。
下面的示例演示如何使用對象初始值設定項初始化新的 StudentName 類型。
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43public 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
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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;
}
}
下面的示例演示如何使用集合初始值設定項初始化一個 StudentName 類型集合。請注意,集合初始值設定項是一系列由逗號分隔的對象初始值設定項。
? 1 2 3 4 5 6 7List<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}
};
使用集合初始值設定項初始化字典
Dictionary<TKey, TValue> 包含鍵/值對集合。 它的 Add 方法采用兩個參數,一個用於鍵,另一個用於值。 若要初始化 Dictionary<TKey, TValue> 或其 Add 方法采用多個參數的任何集合,請將每組參數括在大括號中,如下面的示例所示。
示例
在下面的代碼示例中,使用 StudentName 類型的實例初始化一個 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}}
};
}
請注意集合的每個元素中的兩對大括號。 最內層的大括號括起了 StudentName 的對象初始值,而最外層的大括號括起了將要添加到 studentsDictionary<TKey, TValue> 中的鍵/值對的初始值。 最後,字典的整個集合初始值括在一對大括號內。