using statement equivalent
-
Why and when would you write a code like that? As far as its equivalent is concerned, just see the IL through ILDAsm or reflector and it would make things a bit clear. :)
modified on Monday, June 14, 2010 11:55 AM
-
d@nish wrote:
Why and when would you write a code like that?
When you're interested in keeping track of when "SomeCode" begins and ends -- for logging, profiling, etc.
ShamWow
-
I've been looking around for an answer to this puzzling question, but so far no luck: What's the compiler's
try/finally
equivalent of this code:using (new SomeIDisposableClass())
{
SomeCode();
}Thanks!
ShamWow
Wow, everyone got it wrong :) Debug or Release? The debug code (bugs in Reflector corrected by looking at the MSIL)
SomeIDisposableClass CS$3$0000; bool CS$4$0001; CS$3$0000 = new SomeIDisposableClass();
Label_0007:
try
{
SomeCode();
goto Label_0021;
}
finally
{
Label_0011:
CS$4$0001 = CS$3$0000 == null;
if (CS$4$0001)
{
goto Label_0020;
}
CS$3$0000.Dispose();
Label_0020:;
}
Label_0021:;And yes, it is silly. It's storing the result of the
null
-check The Release codeSomeIDisposableClass CS$3$0000; CS$3$0000 = new SomeIDisposableClass();
Label_0006:
try
{
SomeCode();
goto Label_0017;
}
finally
{
Label_000D:
if (CS$3$0000 == null)
{
goto Label_0016;
}
CS$3$0000.Dispose();
Label_0016:;
}
Label_0017:edit: the rules for
goto
-within-try
are If the goto statement exits one or more try blocks with associated finally blocks, control is initially transferred to the finally block of the innermost try statement. When and if control reaches the end point of a finally block, control is transferred to the finally block of the next enclosing try statement. This process is repeated until the finally blocks of all intervening try statements have been executed. -
Why and when would you write a code like that? As far as its equivalent is concerned, just see the IL through ILDAsm or reflector and it would make things a bit clear. :)
modified on Monday, June 14, 2010 11:55 AM
-
Why and when would you write a code like that? As far as its equivalent is concerned, just see the IL through ILDAsm or reflector and it would make things a bit clear. :)
modified on Monday, June 14, 2010 11:55 AM
-
Wow, everyone got it wrong :) Debug or Release? The debug code (bugs in Reflector corrected by looking at the MSIL)
SomeIDisposableClass CS$3$0000; bool CS$4$0001; CS$3$0000 = new SomeIDisposableClass();
Label_0007:
try
{
SomeCode();
goto Label_0021;
}
finally
{
Label_0011:
CS$4$0001 = CS$3$0000 == null;
if (CS$4$0001)
{
goto Label_0020;
}
CS$3$0000.Dispose();
Label_0020:;
}
Label_0021:;And yes, it is silly. It's storing the result of the
null
-check The Release codeSomeIDisposableClass CS$3$0000; CS$3$0000 = new SomeIDisposableClass();
Label_0006:
try
{
SomeCode();
goto Label_0017;
}
finally
{
Label_000D:
if (CS$3$0000 == null)
{
goto Label_0016;
}
CS$3$0000.Dispose();
Label_0016:;
}
Label_0017:edit: the rules for
goto
-within-try
are If the goto statement exits one or more try blocks with associated finally blocks, control is initially transferred to the finally block of the innermost try statement. When and if control reaches the end point of a finally block, control is transferred to the finally block of the next enclosing try statement. This process is repeated until the finally blocks of all intervening try statements have been executed. -
harold aptroot wrote:
Wow, everyone got it wrong Debug or Release?The debug code (bugs in Reflector corrected by looking at the MSIL)
Bugs? +5 :thumbsup:
I are Troll :suss:
Bugs like this one..
if ((CS$3$0000 == null) != null)
That's not even possible, and the IL for that part is justL\_0011: ldloc.0 L\_0012: ldnull L\_0013: ceq L\_0015: stloc.1 L\_0016: ldloc.1 L\_0017: brtrue.s L\_0020
So Mr Reflector just skipped over a load/store and made up a comparison :wtf:
-
Wow, everyone got it wrong :) Debug or Release? The debug code (bugs in Reflector corrected by looking at the MSIL)
SomeIDisposableClass CS$3$0000; bool CS$4$0001; CS$3$0000 = new SomeIDisposableClass();
Label_0007:
try
{
SomeCode();
goto Label_0021;
}
finally
{
Label_0011:
CS$4$0001 = CS$3$0000 == null;
if (CS$4$0001)
{
goto Label_0020;
}
CS$3$0000.Dispose();
Label_0020:;
}
Label_0021:;And yes, it is silly. It's storing the result of the
null
-check The Release codeSomeIDisposableClass CS$3$0000; CS$3$0000 = new SomeIDisposableClass();
Label_0006:
try
{
SomeCode();
goto Label_0017;
}
finally
{
Label_000D:
if (CS$3$0000 == null)
{
goto Label_0016;
}
CS$3$0000.Dispose();
Label_0016:;
}
Label_0017:edit: the rules for
goto
-within-try
are If the goto statement exits one or more try blocks with associated finally blocks, control is initially transferred to the finally block of the innermost try statement. When and if control reaches the end point of a finally block, control is transferred to the finally block of the next enclosing try statement. This process is repeated until the finally blocks of all intervening try statements have been executed. -
Nice find. :) From now on, I am going to have this in my windows forms application (of course with proper comments):
public class AppCursor : IDisposable
{Cursor \_currentCursor = null; bool \_setDefault; public AppCursor(Cursor cursor, bool setDefault) { \_currentCursor = Cursor.Current; \_setDefault = setDefault; Cursor.Current = cursor; } #region IDisposable Members public void Dispose() { Cursor.Current = (\_setDefault) ? Cursors.Default : \_currentCursor; // Even with disposing the cursor object, form works but am not sure whether it should be there or not. \_currentCursor.Dispose(); \_currentCursor = null; } #endregion }
-
Chris should put that in the advertising. There is an app article for that. :-D
I know the language. I've read a book. - _Madmatt