以下代码段在控制台上打印1到10,但在变量"i"到达int.MaxValue之前不会终止.TIA指出我错过了什么.
class Program
{
public static IEnumerable<int> GetList()
{
int i = 0;
while (i < int.MaxValue)
{
i++;
yield return i;
}
}
static void Main(string[] args)
{
var q = from i in GetList() // keeps calling until i reaches int.MaxValue
where i <= 10
select i;
foreach (int i in q)
Console.WriteLine(i);
}
}
Run Code Online (Sandbox Code Playgroud)