If your initialization list is as simple as a consecutive sequence of values from from
to end
, you can just say
var numbers = Enumerable.Range(from, end - from + 1)
.ToList();
If your initialization list is something a little more intricate that can be defined by a mapping f
from int
to int
, you can say
var numbers = Enumerable.Range(from, end - from + 1)
.Select(n => f(n))
.ToList();
For example:
var primes = Enumerable.Range(1, 10)
.Select(n => Prime(n))
.ToList();
would generate the first ten primes assuming that Prime
is a Func<int, int>
that takes an int n
and returns the n
th prime.