0

I've came across a code snippet like the following:

using (IDbCommand selectCommand = this.createCommand(selectSQL))
using (IDataReader theData = selectCommand.ExecuteReader())
{
  while (theData.Read())
  {
    Phone aPhone = new Phone(...some code here...);
    thePhones.Add(aPhone);
  }
}

Now I am trying to learn the using statement in this context in by interpreting above code into old try/finally statement. From my understanding, the using statement will interpret the code after the brackets as try. In this case, try should enclose all the stuff after (IDbCommand selectCommand = this.createCommand(selectSQL)). However in this code, there is another using statement immediately comes after the first using.

Is it correct to interpret them as a nested try/finally statement in this context?

4
  • 2
    using statement has nothing about try/catch msdn.microsoft.com/en-us/library/yh598w02.aspx Commented Sep 5, 2015 at 4:25
  • I agree with @FabioLuz. It has nothing to do with try catch. I generally wrap my using statements around a try/catch Commented Sep 5, 2015 at 4:27
  • 1
    @FabioLuz This question also has nothing about try/catch, only try/finally :) Commented Sep 5, 2015 at 4:37
  • Oh @ScottChamberlain really sorry, I got it wrong, I misunderstood... thank you! Commented Sep 5, 2015 at 4:40

3 Answers 3

3

Using statements will automatically call Dispose() for any IDisposable object within it declaration. It's important to note that this is not a replacement for any try/catch/finally statements that your own code may need to do. The using statement ensures that Dispose() will be called if any code within it's block throws or not. This is extremely important any time I/O objects are used such as network calls, stream reading, database calls, etc. This will make sure you don't have memory leaks or locked files.

using (IDbCommand selectCommand = this.createCommand(selectSQL))
{
    //an exception or not will call Dispose() on selectCommand

    using (IDataReader theData = selectCommand.ExecuteReader())
    {
        //an exception or not will call Dispose() on theData

        while (theData.Read())
        {
            Phone aPhone = new Phone(...some code here...);
            thePhones.Add(aPhone);
        }
    }
}

The "equivalent" to running this code without using statements would be:

var selectCommand = this.createCommand(selectSQL);
try
{
    var theData = selectCommand.ExecuteReader();
    try
    {
        while (theData.Read())
        {
            Phone aPhone = new Phone(...some code here...);
            thePhones.Add(aPhone);
        }
    }
    finally
    {
        if (theData != null)
        {
            theData.Dispose();
        }
    }
}
finally
{
    if (selectCommand != null)
    {
        selectCommand.Dispose();
    }
}

However, the above code should not be used since the using statement is refined, optimized, guaranteed, etc, but the gist of it is above.

Sign up to request clarification or add additional context in comments.

2 Comments

@Zotta I don't understand what you meen, all 3 of the answers say the same thing, even if you don't use braces the using statements are still nested.
Note that using is slightly more complicated than shown as it saves/checks original value, but for purposes of this question this simplification is not important.
0
using (IDbCommand selectCommand = this.createCommand(selectSQL))
using (IDataReader theData = selectCommand.ExecuteReader())
{
}

At compile time will be converted to

using (IDbCommand selectCommand = this.createCommand(selectSQL))
{
    using (IDataReader theData = selectCommand.ExecuteReader())
    {
    }
}

Just that. And it not related to try catch in anything

Comments

0

Yes, your code is interpreted as

using (IDbCommand selectCommand = this.createCommand(selectSQL))
{
    using (IDataReader theData = selectCommand.ExecuteReader())
    {
      while (theData.Read())
      {
        Phone aPhone = new Phone(...some code here...);
        thePhones.Add(aPhone);
      }
    }
}

which could be thought of as "nested try/finally blocks"

Using multiple using statements is just like with multiple if statements, you could do

if(foo)
if(bar)
{
    DoStuff()
}

Which is the same as

if(foo)
{
    if(bar)
    {
        DoStuff()
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.