ienumerator
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
ienumerator [2021/03/18 01:18] – 73.95.178.156 | ienumerator [2025/01/15 04:35] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 2: | Line 2: | ||
Notes on the underlying implementation of C# generators (which are used in Unity for coroutines). | Notes on the underlying implementation of C# generators (which are used in Unity for coroutines). | ||
+ | |||
+ | You can use SharpLab to view the decompiled C# from a simple generator, to help understand the transformation that is happening. Both .NET and Mono do seem to use the same compiler transformation when turning Generators into anonymous IEnumerator classes. [[https:// | ||
+ | |||
+ | ===== States ===== | ||
+ | |||
+ | Generators are compiled down into a state machine, with states for each '' | ||
+ | * 0: The generator has not yet been run. | ||
+ | * 1: The generator has paused on the first yield return. | ||
+ | * 2: The generator has paused on the second yield return. | ||
+ | * -1: The generator has finished, the generator exited early, or the generator threw an exception. | ||
+ | |||
+ | The state value is set in a field '' | ||
+ | |||
+ | <code c#> | ||
+ | [CompilerGenerated] | ||
+ | private sealed class < | ||
+ | { | ||
+ | private int <> | ||
+ | |||
+ | private object <> | ||
+ | |||
+ | public C <> | ||
+ | // snipped | ||
+ | </ | ||
+ | |||
===== Lambda function DisplayClass classes are initialized at the start of the function ===== | ===== Lambda function DisplayClass classes are initialized at the start of the function ===== | ||
Line 61: | Line 86: | ||
The lambda function is stored as a local variable ''<> | The lambda function is stored as a local variable ''<> | ||
- | ===== All lambdas are piled into a single DisplayClass anonymous type, and share the same instance. | + | **All lambdas are piled into a single DisplayClass anonymous type, and share the same instance.** |
ienumerator.1616030329.txt.gz · Last modified: 2025/01/15 04:35 (external edit)