0

Add properties while Initialize Objects by Using an Object Initializer . But how can we add DataCollection property?

Example:

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()
        };

So if we want to add property for "Subjects" how can we add in the above condition?

Generally we can do like below.

     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 に答える