I must be reinventing the wheel here - but I've searched and I can't find anything quite the same...
Here's my code for creating a sequence of zero or more objects that have a default constructor:
public static IEnumerable<T> CreateSequence<T>(int n) where T: new()
{
for (int i = 0; i < n; ++i)
{
yield return new T();
}
}
My question is quite simple: Is there a Linq equivalent of this I should be using?
Try this:
Enumerable.Range(1,count).Select(_ => new T());
Enumerable.Range
will give you the current number from the specified range as parameter, but you can simply ignore that (named as _
in the example).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With