如何:使用对象初始值设定项初始化对象

 

可以使用对象初始值设定项以声明方式初始化类型对象,而无需显式调用类型的构造函数。

下面的示例演示如何将对象初始值设定项用于命名对象。  编译器通过先访问默认实例构造函数然后处理成员初始化处理对象初始值设定项。  因此,如果默认构造函数在类中声明为 private,那么需要公共访问权的对象初始值设定项将失败。  

如果定义匿名类型,则必须使用对象初始值设定项。  有关更多信息,请参见如何:在查询中返回元素属性的子集(C# 编程指南)  

示例

下面的示例演示如何使用对象初始值设定项初始化新的 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());
    }

    // Output:
    // 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}
};

编译代码

若要运行这段代码,请将该类复制并粘贴到已经在 Visual Studio 中创建的 Visual C# 控制台应用程序项目中。  有关更多信息,请参见How to: Create a LINQ Project  

请参阅