含 `yield return` 的方法/屬性,編譯器生成巢狀類別實作 `IEnumerator
// 原始碼
public IEnumerator GetEnumerator()
{
yield return "New York";
yield return "Paris";
yield return "London";
}
// 編譯器生成偽代碼
public class MyCollection : IEnumerable
{
public IEnumerator GetEnumerator()
{
var impl = new GetEnumerator$0003__IEnumeratorImpl();
impl.<this> = this;
return impl;
}
private class GetEnumerator$0003__IEnumeratorImpl : IEnumerator
{
public MyCollection <this>;
string $_current;
// 狀態機欄位...
public string Current => $_current;
public bool MoveNext() { /* 狀態機邏輯 */ }
public void Dispose() { /* 清理 */ }
}
}