欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

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

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次

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

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

下面的示例演示如何使用對象初始值設(shè)定項初始化新的 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è)定項初始化一個 StudentName 類型集合。請注意,集合初始值設(shè)定項是一系列由逗號分隔的對象初始值設(shè)定項。

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è)定項初始化字典

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}}
  };
}

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

上一篇:C#提高編程能力的50個要點總結(jié)

欄    目:C#教程

下一篇:淺談C#指針問題

本文標(biāo)題:詳解C#中使用對象或集合的初始值設(shè)定項初始化的操作

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6711.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有