While exploring an assembly in reflector I stumbled upon a fault
keyword in a compiler generated class.
Do any of you know the meaning if this keyword?
C#
private bool MoveNext()
{
bool flag;
try
{
// [...]
}
fault
{
this.Dispose();
}
return flag;
}
vb.net
Private Function MoveNext() As Boolean
Dim flag As Boolean
Try
' [...]
Fault
Me.Dispose
End Try
Return flag
End Function
Do any of you know the meaning if this keyword?
Yes. It's not valid C#, but in IL it's the equivalent of finally
, but only if an exception has been thrown.
There's no direct correlation in C#, which is why the decompiler can't decompile it to proper C#. You could emulate it with something like:
bool success = false;
try
{
... stuff ...
success = true; // This has to occur on all "normal" ways of exiting the
// block, including return statements.
}
finally
{
if (!success)
{
Dispose();
}
}
I mention it in my iterator block implementation details article which looks relevant to your particular example :)
See more on this question at Stackoverflow