在 JavaScript 中,我可以创建一个生成器,其行为类似于以下内容:
但是根据我目前对C#的了解,上述内容不能按照我的意图工作,而是会导致无限循环。
function* idMaker(){
var index = 0;
while(true)
yield index++;
}
var gen = idMaker();
console.log(gen.next().value); // 0
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
请问C#中等价的代码是什么?
我在想这段代码是否可行:
static System.Collections.Generic.IEnumerable<int> MakeId()
{
int index = 0;
while (true)
yield return index++;
}
但是根据我目前对C#的了解,上述内容不能按照我的意图工作,而是会导致无限循环。
while(true)
会锁定您的应用程序,直到您的RAM用尽。 - Manfred Radlwimmer