c# - Creating a list filled with new instances of an object -


what's best way create list arbitrary number of instances of same object? i.e there more compact or efficient way following?

static list<myobj> myobjs = enumerable.range(0, 100)     .select(i => new myobj())     .tolist(); 

(enumerable.repeat give me ten references same object, don't think work.)

this wouldn't hard implement iterator:

ienumerable<t> createitems<t> (int count) t : new() {     return createitems(count, () => new t()); }  ienumerable<t> createitems<t> (int count, func<t> creator) {     (int = 0; < count; i++) {         yield return creator();     } } 

Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -