当前位置: 移动技术网 > IT编程>开发语言>c# > 详解C#中使用对象或集合的初始值设定项初始化的操作

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

2019年07月18日  | 移动技术网IT编程  | 我要评论

使用对象初始值设定项初始化对象

可以使用对象初始值设定项以声明方式初始化类型对象,而无需显式调用类型的构造函数。
下面的示例演示如何将对象初始值设定项用于命名对象。编译器通过先访问默认实例构造函数然后处理成员初始化处理对象初始值设定项。因此,如果默认构造函数在类中声明为 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> 中的键/值对的初始值。 最后,字典的整个集合初始值括在一对大括号内。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网