详解C#中使用对象或集合的初始值设定项初始化的操作

 更新时间:2016年01月31日 14:49:38   投稿:goldensun  
这篇文章主要介绍了详解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> 中的键/值对的初始值。 最后,字典的整个集合初始值括在一对大括号内。

相关文章

  • C#注释的一些使用方法浅谈

    C#注释的一些使用方法浅谈

    C#注释的一些使用方法浅谈,需要的朋友可以参考一下
    2013-04-04
  • C# ArrayList、HashSet、HashTable、List、Dictionary的区别详解

    C# ArrayList、HashSet、HashTable、List、Dictionary的区别详解

    这篇文章主要介绍了C# ArrayList、HashSet、HashTable、List、Dictionary的区别的相关知识点内容,有需要朋友们参考下。
    2019-08-08
  • C#异步编程由浅入深(三)之详解Awaiter

    C#异步编程由浅入深(三)之详解Awaiter

    这篇文章主要介绍了C#异步编程由浅入深(三)之详解Awaiter,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • C# 多进程打开PPT的示例教程

    C# 多进程打开PPT的示例教程

    这篇文章主要介绍了C# 多进程打开PPT的示例教程,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下
    2021-01-01
  • C# xml序列化实现及遇到的坑

    C# xml序列化实现及遇到的坑

    在C#中,当我们需要将对象存储到文件或通过网络发送时,我们可以使用XML序列化将C#对象转换为XML文档,以便于存储、传输和还原,本文主要介绍了C# xml序列化实现及遇到的坑,感兴趣的可以了解一下
    2023-09-09
  • 通过C#实现在Word中插入或删除分节符

    通过C#实现在Word中插入或删除分节符

    在Word中,分节符是一种强大的工具,用于将文档分成不同的部分,每个部分可以有独立的页面设置,如页边距、纸张方向、页眉和页脚等,本文将介绍如何使用一个免费的.NET库通过C#实现插入或删除Word分节符,需要的朋友可以参考下
    2024-08-08
  • C#中的in参数与性能分析详解

    C#中的in参数与性能分析详解

    这篇文章主要给大家介绍了关于C#中in参数与性能分析的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • 浅析C#中goto跳转语句的用法

    浅析C#中goto跳转语句的用法

    在我们日常工作中常用的C#跳转语句有break、continue、return,但是还有一个C#跳转语句很多同学可能都比较的陌生就是goto,下面我们就来看看goto跳转语句的用法吧
    2024-03-03
  • unity 实现摄像机绕某点旋转一周

    unity 实现摄像机绕某点旋转一周

    这篇文章主要介绍了unity 实现摄像机绕某点旋转一周,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • 理解C#中的枚举(简明易懂)

    理解C#中的枚举(简明易懂)

    这篇文章主要介绍了理解C#中的枚举(简明易懂),本文讲解了枚举的优点、枚举说明、枚举的类型、枚举的使用建议等内容,需要的朋友可以参考下
    2015-05-05

最新评论