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
Post a Comment