Pregunta

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#" });
¿Fue útil?

Solución

Student myStudent = new Student
        {
            FirstName = "John",
            LastName = "Something"
            Subjects = {
                           "Subject1",
                           "Subject2",
                           "Subject3",
                       }
        };
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top