キーワードコンパイラを使用するとyield
、ネストされたクラスが生成されます。このクラスは、を実装し、IEnumerable
すべてのコンテキストデータを格納します。IEnumerator
IDisposable
[CompilerGenerated]
private sealed class <Read>d__0 : IEnumerable<YourObject>, IEnumerable, IEnumerator<YourObject>, IEnumerator, IDisposable
{
// Fields
private int <>1__state;
private YourObject <>2__current;
public string <>3__filename;
public Foo <>4__this;
private int <>l__initialThreadId;
public FileStream <filestream>5__1;
public string <line>5__3;
public StreamReader <reader>5__2;
public string filename;
// Methods
[DebuggerHidden]
public <Read>d__0(int <>1__state);
private void <>m__Finally4();
private void <>m__Finally5();
private bool MoveNext();
[DebuggerHidden]
IEnumerator<YourObject> IEnumerable<YourObject>.GetEnumerator();
[DebuggerHidden]
IEnumerator IEnumerable.GetEnumerator();
[DebuggerHidden]
void IEnumerator.Reset();
void IDisposable.Dispose();
// Properties
YourObject IEnumerator<YourObject>.Current { [DebuggerHidden] get; }
object IEnumerator.Current { [DebuggerHidden] get; }
}
ご覧のとおり、yieldingメソッドのコンテキストからのすべてのローカル変数は、この生成されたクラスのフィールドに移動されます。興味深い方法はm_Finally
、名前に次のようなものがあります。
private void <>m__Finally4()
{
this.<>1__state = -1;
if (this.<filestream>5__1 != null)
{
this.<filestream>5__1.Dispose();
}
}
ご覧のとおり、これらのメソッドは使い捨てオブジェクト(FileStream
およびStreamReader
)を破棄します。いつ呼ばれた?列挙の最後、またはDispose
呼び出されたとき:
private bool MoveNext()
{
bool CS$1$0000;
try
{
int CS$4$0001 = this.<>1__state;
if (CS$4$0001 != 0)
{
if (CS$4$0001 != 3)
{
goto Label_00AB;
}
goto Label_0074;
}
this.<>1__state = -1;
this.<filestream>5__1 = new FileStream(this.filename, FileMode.Open);
this.<>1__state = 1;
this.<reader>5__2 = new StreamReader(this.<filestream>5__1);
this.<>1__state = 2;
while ((this.<line>5__3 = this.<reader>5__2.ReadLine()) != null)
{
this.<>2__current = new YourObject(this.<line>5__3);
this.<>1__state = 3;
return true;
Label_0074:
this.<>1__state = 2;
}
this.<>m__Finally5();
this.<>m__Finally4();
Label_00AB:
CS$1$0000 = false;
}
fault
{
this.System.IDisposable.Dispose();
}
return CS$1$0000;
}
void IDisposable.Dispose()
{
switch (this.<>1__state)
{
case 1:
case 2:
case 3:
try
{
switch (this.<>1__state)
{
case 2:
case 3:
break;
default:
break;
}
try
{
}
finally
{
this.<>m__Finally5();
}
}
finally
{
this.<>m__Finally4();
}
break;
}
}
First()
の実装を見ると、次のように表示されます。最初のアイテムを返した後Enumerable
に呼び出されます。Dispose
using (IEnumerator<TSource> enumerator = source.GetEnumerator())
{
if (enumerator.MoveNext())
{
return enumerator.Current;
}
}
したがってDispose
、自動生成されたクラスが呼び出され、破棄が必要なすべてのローカル変数がメソッドの呼び出しによって破棄されm_Finally
ます。
ところで(LINQでの使用法ではありません)foreachステートメントの実装を見ると、列挙子が列挙後に破棄されていることがわかります。したがって、生成されたクラスは、例外Dispose
の場合でも呼び出されます。break