Tag: reflector

有人可以解释这个懒惰的评估代码吗?

所以,这个问题刚刚被问到: 如何处理“无限”IEnumerable? 我的示例代码: public static void Main(string[] args) { foreach (var item in Numbers().Take(10)) Console.WriteLine(item); Console.ReadKey(); } public static IEnumerable Numbers() { int x = 0; while (true) yield return x++; } 有人可以解释为什么这是懒惰的评估? 我在Reflector中查找了这段代码,而且比起初时我更困惑。 reflection器输出: public static IEnumerable Numbers() { return new d__0(-2); } 对于numbers方法,看起来为该表达式生成了一个新类型: [DebuggerHidden] public d__0(int 1__state) { this.1__state = 1__state; this.l__initialThreadId = […]