C#, weird optimization
        Posted  
        
            by Snake
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Snake
        
        
        
        Published on 2010-05-07T12:53:22Z
        Indexed on 
            2010/05/07
            12:58 UTC
        
        
        Read the original article
        Hit count: 368
        
Hi,
I'm trying to read my compiled C# code.
this is my code:
using(OleDbCommand insertCommand = new OleDbCommand("...", connection))
{
   // do super stuff
}
But!
We all know that a using gets translated to this:
{
    OleDbCommand insertCommand = new OleDbCommand("...", connection)
    try
    {
        //do super stuff
    }
    finally
    {
        if(insertCommand != null)
            ((IDisposable)insertCommand).Dispose();
    }
}
(since OleDbCommand is a reference type).
But when I decompile my assembly (compiled with .NET 2.0) I get this in Resharper:
try
{
    insertCommand = new OleDbCommand("", connection);
Label_0017:
    try
    {
       //do super stuff
    }
    finally
    {
    Label_0111:
        if ((insertCommand == null) != null)
        {
            goto Label_0122;
        }
        insertCommand.Dispose();
    Label_0122:;
    }
I'm talking about this line: if ((insertCommand == null) != null). True is not null, it never is, nor is false.
So how is my object disposed properly? WTF?
Thanks!
-Kristof
© Stack Overflow or respective owner