詳解C#中使用對象或集合的初始值設定項初始化的操作
使用對象初始值設定項初始化對象
可以使用對象初始值設定項以聲明方式初始化類型對象,而無需顯式調用類型的構造函數(shù)。
下面的示例演示如何將對象初始值設定項用于命名對象。編譯器通過先訪問默認實例構造函數(shù)然后處理成員初始化處理對象初始值設定項。因此,如果默認構造函數(shù)在類中聲明為 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 方法采用兩個參數(shù),一個用于鍵,另一個用于值。 若要初始化 Dictionary<TKey, TValue> 或其 Add 方法采用多個參數(shù)的任何集合,請將每組參數(shù)括在大括號中,如下面的示例所示。
示例
在下面的代碼示例中,使用 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> 中的鍵/值對的初始值。 最后,字典的整個集合初始值括在一對大括號內。
上一篇:C#提高編程能力的50個要點總結
欄 目:C#教程
下一篇:淺談C#指針問題
本文標題:詳解C#中使用對象或集合的初始值設定項初始化的操作
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6711.html
您可能感興趣的文章
- 01-10C#通過反射獲取當前工程中所有窗體并打開的方法
- 01-10C#實現(xiàn)Winform中打開網頁頁面的方法
- 01-10C#實現(xiàn)由四周向中心縮小的窗體退出特效
- 01-10Extjs4如何處理后臺json數(shù)據(jù)中日期和時間
- 01-10C#使用Dispose模式實現(xiàn)手動對資源的釋放
- 01-10C#3.0使用EventLog類寫Windows事件日志的方法
- 01-10C#中DataGridView常用操作實例小結
- 01-10C#編程獲取資源文件中圖片的方法
- 01-10C#使用windows服務開啟應用程序的方法
- 01-10asp.net中XML如何做增刪改查操作


閱讀排行
本欄相關
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關于ASP網頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 01-10C#中split用法實例總結
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-10delphi制作wav文件的方法


