欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解C#中使用對象或集合的初始值設(shè)定項(xiàng)初始化的操作

 更新時間:2016年01月31日 14:49:38   投稿:goldensun  
這篇文章主要介紹了詳解C#中使用對象或集合的初始值設(shè)定項(xiàng)初始化的操作,文中分別講了對對象和字典的初始化,需要的朋友可以參考下

使用對象初始值設(shè)定項(xiàng)初始化對象

可以使用對象初始值設(shè)定項(xiàng)以聲明方式初始化類型對象,而無需顯式調(diào)用類型的構(gòu)造函數(shù)。
下面的示例演示如何將對象初始值設(shè)定項(xiàng)用于命名對象。編譯器通過先訪問默認(rèn)實(shí)例構(gòu)造函數(shù)然后處理成員初始化處理對象初始值設(shè)定項(xiàng)。因此,如果默認(rèn)構(gòu)造函數(shù)在類中聲明為 private,那么需要公共訪問權(quán)的對象初始值設(shè)定項(xiàng)將失敗。

下面的示例演示如何使用對象初始值設(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)初始化一個 StudentName 類型集合。請注意,集合初始值設(shè)定項(xiàng)是一系列由逗號分隔的對象初始值設(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> 包含鍵/值對集合。 它的 Add 方法采用兩個參數(shù),一個用于鍵,另一個用于值。 若要初始化 Dictionary<TKey, TValue> 或其 Add 方法采用多個參數(shù)的任何集合,請將每組參數(shù)括在大括號中,如下面的示例所示。
示例
在下面的代碼示例中,使用 StudentName 類型的實(shí)例初始化一個 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}}
  };
}

請注意集合的每個元素中的兩對大括號。 最內(nèi)層的大括號括起了 StudentName 的對象初始值,而最外層的大括號括起了將要添加到 studentsDictionary<TKey, TValue> 中的鍵/值對的初始值。 最后,字典的整個集合初始值括在一對大括號內(nèi)。

相關(guān)文章

  • c#中LINQ的基本用法(二)

    c#中LINQ的基本用法(二)

    這篇文章介紹了c#中LINQ的基本用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下的相關(guān)資料
    2022-04-04
  • 詳解從零開始---用C#制作掃雷游戲

    詳解從零開始---用C#制作掃雷游戲

    這篇文章主要介紹了詳解從零開始---用C#制作掃雷游戲,非常具有實(shí)用價值,需要的朋友可以參考下
    2017-06-06
  • C#操作windows系統(tǒng)進(jìn)程的方法

    C#操作windows系統(tǒng)進(jìn)程的方法

    這篇文章主要介紹了C#操作windows系統(tǒng)進(jìn)程的方法,涉及C#針對windows操作系統(tǒng)進(jìn)程的創(chuàng)建與關(guān)閉的技巧,需要的朋友可以參考下
    2015-04-04
  • C#如何訪問共享文件夾或者磁盤

    C#如何訪問共享文件夾或者磁盤

    這篇文章主要為大家詳細(xì)介紹了C#訪問共享文件夾或者磁盤,需要用戶名密碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • C#通用郵件發(fā)送類分享

    C#通用郵件發(fā)送類分享

    這篇文章主要介紹了C#通用郵件發(fā)送類分享,本文類比較特別的一點(diǎn)是涵蓋了國內(nèi)大多數(shù)的常用郵箱,需要的朋友可以參考下
    2015-05-05
  • 基于WPF開發(fā)txt閱讀器

    基于WPF開發(fā)txt閱讀器

    這篇文章主要為大家詳細(xì)介紹了如何基于WPF開發(fā)一個簡單的txt閱讀器,可以滿足文本文件的讀寫和保存,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-06-06
  • Unity3D實(shí)現(xiàn)人物移動示例

    Unity3D實(shí)現(xiàn)人物移動示例

    這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)人物移動示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-01-01
  • C#在驗(yàn)證文件共享模式下實(shí)現(xiàn)多線程文件寫入

    C#在驗(yàn)證文件共享模式下實(shí)現(xiàn)多線程文件寫入

    這篇文章主要為大家詳細(xì)介紹了C#在驗(yàn)證文件共享模式下實(shí)現(xiàn)多線程文件寫入的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下
    2024-01-01
  • C#實(shí)現(xiàn)掃雷游戲

    C#實(shí)現(xiàn)掃雷游戲

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • C#特性 迭代器(下) yield以及流的延遲計(jì)算

    C#特性 迭代器(下) yield以及流的延遲計(jì)算

    這篇文章主要介紹了C#特性 迭代器(下) yield以及流的延遲計(jì)算,需要的朋友可以參考下
    2014-12-12

最新評論