詳解C#中應用對象或聚集的初始值設定項初始化的操作。本站提示廣大學習愛好者:(詳解C#中應用對象或聚集的初始值設定項初始化的操作)文章只能為提供參考,不一定能成為您想要的結果。以下是詳解C#中應用對象或聚集的初始值設定項初始化的操作正文
應用對象初始值設定項初始化對象
可使用對象初始值設定項以聲明方法初始化類型對象,而無需顯式挪用類型的結構函數。
上面的示例演示若何將對象初始值設定項用於定名對象。編譯器經由過程先拜訪默許實例結構函數然後處置成員初始化處置對象初始值設定項。是以,假如默許結構函數在類中聲明為 private,那末須要公共拜訪權的對象初始值設定項將掉敗。
上面的示例演示若何應用對象初始值設定項初始化新的 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;
}
}
上面的示例演示若何應用聚集初始值設定項初始化一個 StudentName 類型聚集。請留意,聚集初始值設定項是一系列由逗號分隔的對象初始值設定項。
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}
};
應用聚集初始值設定項初始化字典
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> 中的鍵/值對的初始值。 最初,字典的全部聚集初始值括在一對年夜括號內。