Linked Questions

Popular Questions

How to elegantly populate a heterogeneous array in c#

Asked by At

I have 3 different classes (1 parent and 2 children), and I have created a heterogeneous array:

Parent[] parentArray =  new Parent[9];

I would like to populate this array with 3 Parent objects, 3 child1 objects, and 3 child2 objects, in that order.

I would like to know if there is a more elegant way to do it than just doing this:

parentArray[0] = new Parent();

parentArray[1] = new Parent();

parentArray[2] = new Parent();

parentArray[3] = new Child1();

parentArray[4] = new Child1();


parentArray[9] = new Child2();


Related Questions