相关疑难解决方法(0)

实现C#yield语句的算法

我想自己弄明白,但我想知道将带有yield语句的函数转换为枚举器状态机的算法是什么?例如,C#如何转变:

IEnumerator<string> strings(IEnumerable<string> args)
 { IEnumerator<string> enumerator2 = getAnotherEnumerator();     
   foreach(var arg in arg) 
    { enumerator2.MoveNext();
      yield return arg+enumerator.Current;
    } 
 }
Run Code Online (Sandbox Code Playgroud)

进入这个:

bool MoveNext()
 { switch (this.state)
    {
        case 0:
            this.state = -1;
            this.enumerator2 = getAnotherEnumerator();
            this.argsEnumerator = this.args.GetEnumerator();
            this.state = 1;
            while (this.argsEnumerator.MoveNext())
            {
                this.arg = this.argsEnumerator.Current;
                this.enumerator2.MoveNext();
                this.current = this.arg + this.enumerator2.Current;
                this.state = 2;
                return true;

              state1:
                this.state = 1;
            }
            this.state = -1;
            if (this.argsEnumerator != null) this.argsEnumerator.Dispose();
            break;

        case 2:
            goto state1;
    }
    return …
Run Code Online (Sandbox Code Playgroud)

c# compiler-construction algorithm iterator state-machine

31
推荐指数
3
解决办法
3843
查看次数