ienumerator
This is an old revision of the document!
IEnumerator
Notes on the underlying implementation of C# generators (which are used in Unity for coroutines).
Lambda function DisplayClass classes are initialized at the start of the function
The following code, in SharpLab decompiles it as:
using System; using System.Collections; public class C { public IEnumerator M() { int x = 5; yield return Test(() => x > 5); yield return 5; yield return 5; yield return 5; } public IEnumerator Test(Func<bool> func) { yield return func(); } }
.. snipped private bool MoveNext() { switch (<>1__state) { default: return false; case 0: <>1__state = -1; <>8__1 = new <>c__DisplayClass0_0(); <>8__1.x = 5; <>2__current = 5; <>1__state = 1; return true; case 1: <>1__state = -1; <>2__current = 5; <>1__state = 2; return true; case 2: <>1__state = -1; <>2__current = <>4__this.Test(new Func<bool>(<>8__1.<M>b__0)); <>1__state = 3; return true; case 3: <>1__state = -1; <>2__current = 5; <>1__state = 4; return true; case 4: <>1__state = -1; return false; } }
The lambda function is stored as a local variable <>8__1
in the generated IEnumerator class. However, the instance is created at the start of the function, not just before usage, as one might expect.
ienumerator.1616030016.txt.gz · Last modified: 2025/01/15 04:35 (external edit)