If you want to enumerate a sequence of numbers (IEnumerable<int>
) from 0
to a variable end
, then try
Enumerable.Range(0, ++end);
In explanation, to get a sequence of numbers from 0 to 1000, you want the sequence to start at 0 (remembering that there are 1001 numbers between 0 and 1000, inclusive).
If you want an unlimited linear series, you could write a function like
IEnumerable<int> Series(int k = 0, int n = 1, int c = 1)
{
while (true)
{
yield return k;
k = (c * k) + n;
}
}
which you could use like
var ZeroTo1000 = Series().Take(1001);
If you want a function you can call repeatedly to generate incrementing numbers, perhaps you want somthing like.
using System.Threading;
private static int orderNumber = 0;
int Seq()
{
return Interlocked.Increment(ref orderNumber);
}
When you call Seq()
it will return the next order number and increment the counter.