0

在使用 Object Initializer 初始化对象时添加属性。但是我们如何添加 DataCollection 属性呢?

例子:

class Student{
          public string FirstName{ get; set} ;
          public string LastName{ get; set};
          public DataCollection<string> Subjects{ get; set} ; 

}

 Student myStudent = new Student
        {
            FirstName = "John",
            LastName = "Something"
            //Subjects.AddRange()
        };

那么如果我们想为“Subjects”添加属性,我们如何在上述条件下添加呢?

一般我们可以像下面这样。

     Student clsStudent  = new Student();
     clsStudent.FirstName  = "Foo";
     clsStudent.LastName  = "other";
     clsStudent.Values.AddRange(new string[] { "c#" });
4

1 回答 1

1
Student myStudent = new Student
        {
            FirstName = "John",
            LastName = "Something"
            Subjects = {
                           "Subject1",
                           "Subject2",
                           "Subject3",
                       }
        };
于 2013-03-08T19:21:56.607 回答