最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

相關(guān)文章

  • C#注釋的一些使用方法淺談

    C#注釋的一些使用方法淺談

    C#注釋的一些使用方法淺談,需要的朋友可以參考一下
    2013-04-04
  • C# ArrayList、HashSet、HashTable、List、Dictionary的區(qū)別詳解

    C# ArrayList、HashSet、HashTable、List、Dictionary的區(qū)別詳解

    這篇文章主要介紹了C# ArrayList、HashSet、HashTable、List、Dictionary的區(qū)別的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要朋友們參考下。
    2019-08-08
  • C#異步編程由淺入深(三)之詳解Awaiter

    C#異步編程由淺入深(三)之詳解Awaiter

    這篇文章主要介紹了C#異步編程由淺入深(三)之詳解Awaiter,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • C# 多進(jìn)程打開PPT的示例教程

    C# 多進(jìn)程打開PPT的示例教程

    這篇文章主要介紹了C# 多進(jìn)程打開PPT的示例教程,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2021-01-01
  • C#?xml序列化實(shí)現(xiàn)及遇到的坑

    C#?xml序列化實(shí)現(xiàn)及遇到的坑

    在C#中,當(dāng)我們需要將對(duì)象存儲(chǔ)到文件或通過(guò)網(wǎng)絡(luò)發(fā)送時(shí),我們可以使用XML序列化將C#對(duì)象轉(zhuǎn)換為XML文檔,以便于存儲(chǔ)、傳輸和還原,本文主要介紹了C#?xml序列化實(shí)現(xiàn)及遇到的坑,感興趣的可以了解一下
    2023-09-09
  • 通過(guò)C#實(shí)現(xiàn)在Word中插入或刪除分節(jié)符

    通過(guò)C#實(shí)現(xiàn)在Word中插入或刪除分節(jié)符

    在Word中,分節(jié)符是一種強(qiáng)大的工具,用于將文檔分成不同的部分,每個(gè)部分可以有獨(dú)立的頁(yè)面設(shè)置,如頁(yè)邊距、紙張方向、頁(yè)眉和頁(yè)腳等,本文將介紹如何使用一個(gè)免費(fèi)的.NET庫(kù)通過(guò)C#實(shí)現(xiàn)插入或刪除Word分節(jié)符,需要的朋友可以參考下
    2024-08-08
  • C#中的in參數(shù)與性能分析詳解

    C#中的in參數(shù)與性能分析詳解

    這篇文章主要給大家介紹了關(guān)于C#中in參數(shù)與性能分析的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • 淺析C#中g(shù)oto跳轉(zhuǎn)語(yǔ)句的用法

    淺析C#中g(shù)oto跳轉(zhuǎn)語(yǔ)句的用法

    在我們?nèi)粘9ぷ髦谐S玫腃#跳轉(zhuǎn)語(yǔ)句有break、continue、return,但是還有一個(gè)C#跳轉(zhuǎn)語(yǔ)句很多同學(xué)可能都比較的陌生就是goto,下面我們就來(lái)看看goto跳轉(zhuǎn)語(yǔ)句的用法吧
    2024-03-03
  • unity 實(shí)現(xiàn)攝像機(jī)繞某點(diǎn)旋轉(zhuǎn)一周

    unity 實(shí)現(xiàn)攝像機(jī)繞某點(diǎn)旋轉(zhuǎn)一周

    這篇文章主要介紹了unity 實(shí)現(xiàn)攝像機(jī)繞某點(diǎn)旋轉(zhuǎn)一周,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-04-04
  • 理解C#中的枚舉(簡(jiǎn)明易懂)

    理解C#中的枚舉(簡(jiǎn)明易懂)

    這篇文章主要介紹了理解C#中的枚舉(簡(jiǎn)明易懂),本文講解了枚舉的優(yōu)點(diǎn)、枚舉說(shuō)明、枚舉的類型、枚舉的使用建議等內(nèi)容,需要的朋友可以參考下
    2015-05-05

最新評(píng)論

揭阳市| 盱眙县| 马尔康县| 济南市| 玛纳斯县| 通榆县| 林西县| 大城县| 南阳市| 社旗县| 岢岚县| 温泉县| 玉龙| 娱乐| 安多县| 临安市| 昌黎县| 榆中县| 宁陕县| 濮阳市| 武胜县| 纳雍县| 普兰店市| 合水县| 澎湖县| 舞阳县| 平泉县| 陇川县| 海安县| 房产| 界首市| 西和县| 荣成市| 兴城市| 新宁县| 渭源县| 崇义县| 新晃| 诏安县| 恩施市| 灵武市|