详解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#中LINQ的基本用法(二)

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

    这篇文章介绍了c#中LINQ的基本用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下的相关资料
    2022-04-04
  • 详解从零开始---用C#制作扫雷游戏

    详解从零开始---用C#制作扫雷游戏

    这篇文章主要介绍了详解从零开始---用C#制作扫雷游戏,非常具有实用价值,需要的朋友可以参考下
    2017-06-06
  • C#操作windows系统进程的方法

    C#操作windows系统进程的方法

    这篇文章主要介绍了C#操作windows系统进程的方法,涉及C#针对windows操作系统进程的创建与关闭的技巧,需要的朋友可以参考下
    2015-04-04
  • C#如何访问共享文件夹或者磁盘

    C#如何访问共享文件夹或者磁盘

    这篇文章主要为大家详细介绍了C#访问共享文件夹或者磁盘,需要用户名密码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • C#通用邮件发送类分享

    C#通用邮件发送类分享

    这篇文章主要介绍了C#通用邮件发送类分享,本文类比较特别的一点是涵盖了国内大多数的常用邮箱,需要的朋友可以参考下
    2015-05-05
  • 基于WPF开发txt阅读器

    基于WPF开发txt阅读器

    这篇文章主要为大家详细介绍了如何基于WPF开发一个简单的txt阅读器,可以满足文本文件的读写和保存,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-06-06
  • Unity3D实现人物移动示例

    Unity3D实现人物移动示例

    这篇文章主要为大家详细介绍了Unity3D实现人物移动示例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-01-01
  • C#在验证文件共享模式下实现多线程文件写入

    C#在验证文件共享模式下实现多线程文件写入

    这篇文章主要为大家详细介绍了C#在验证文件共享模式下实现多线程文件写入的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下
    2024-01-01
  • C#实现扫雷游戏

    C#实现扫雷游戏

    这篇文章主要为大家详细介绍了C#实现扫雷游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • C#特性 迭代器(下) yield以及流的延迟计算

    C#特性 迭代器(下) yield以及流的延迟计算

    这篇文章主要介绍了C#特性 迭代器(下) yield以及流的延迟计算,需要的朋友可以参考下
    2014-12-12

最新评论