IEnumerable <t> GetEnumerator()exexcution </t>

时间:2011-01-02 22:22:50

标签: c# .net-3.5 ienumerable

所以我的一个类实现了IQueryable,它需要GetEnumerator方法,代码如下所示:

        public IEnumerator<T> GetEnumerator() 
        {
             this.ParseExpression(this.expression);
             return this.GetResults()

        }

        private IEnumerator<T> GetResults() 
        {
              //Processes the expression tree.

              T t = Activator.CreateInstance(typeof(T));
              yield return T;
        }

奇怪的部分是当控件进入第一个方法时它跳到它的末尾(在它进入GetResults()方法之前的结束大括号。这是否意味着隐式地在不同的线程上执行GetResults()方法由编译器实现IEnumerable?

的事实

1 个答案:

答案 0 :(得分:4)

不,这意味着编译器添加了更多代码,您没有看到执行,因为没有源代码代表它。

使用yield时,编译器会为您创建枚举器的实现。调用GetResults并不会真正调用您的方法,而是调用该枚举器的构造函数。当你从枚举器开始读取你的方法第一次被调用时。

相关问题