Is there a function in C# that returns an IEnumerator
of the infinite sequence of integers [0, 1, 2, 3, 4, 5 ...]
?
I'm currently doing
Enumerable.Range (0, 1000000000).Select (x => x * x).TakeWhile (x => (x <= limit))
to enumerate all squares up to limit
. I realize that this is effective, but if there's a built-in function that just counts up from 0
, I would prefer to use it.
You could roll your own.
IEnumerable<BigInteger> Infinite() {
BigInteger value = 0;
while (true) {
yield return value++;
}
}
Edit
Why dont you just pass limit in to Range
? This might be off by one...
Enumerable.Range(0, limit).Select(x => x * x);
I was wrong about this edit.
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