Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#, weird optimization

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).

Let's say insertCommand IS null. Then the first part returns true. (true != null) returns true. So Then the disposing is still skipped? Weird, very weird.

If I paste this in Visual Studio, Resharper already warns me: Expression is always true...

Thanks!

-Kristof

like image 601
Snake Avatar asked May 07 '10 12:05

Snake


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

The decompiler has a bug. This line

if ((insertCommand == null) != null) 

should have been decompiled to

if ((insertCommand == null) != false)

which, though needlessly verbose, is at least correct code.

The decompiler probably does this unnecessarily verbose version because the C# compiler often chooses to emit

if (x)
   Y();
Z();

as if you'd written

if (!x)
    goto L;
Y();
L: Z();

Since the code generated for both programs is the same, the decompiler doesn't always know which one is the more sensible code to display.

The reason for the unexpected "!= false" is because when we generate IL that tests whether something is true, the fastest and most compact code we can generate is to test whether it is not false. False is represented as zero in IL, and there's a cheap instruction for "is this thing zero?"

like image 82
Eric Lippert Avatar answered Oct 27 '22 03:10

Eric Lippert