C# 中是否有一个函数返回无限整数序列 [0, 1, 2, 3, 4, 5 ...]
的 IEnumerator
?
我现在正在做
Enumerable.Range (0, 1000000000).Select (x => x * x).TakeWhile (x => (x <= limit))
枚举所有小于 limit
的方 block 。我意识到这很有效,但如果有一个从 0
开始计数的内置函数,我更愿意使用它。
最佳答案
您可以自己滚动。
IEnumerable<BigInteger> Infinite() {
BigInteger value = 0;
while (true) {
yield return value++;
}
}
编辑
<罢工>
你为什么不把限制传递给Range
?这可能会差一个...
Enumerable.Range(0, limit).Select(x => x * x);
罢工><罢工>罢工> 我对这次编辑有误。
关于c# - 如何在 C# 4.0 中枚举无限整数序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7303027/