Question

This is the first time that I'll be using nested using. Am I doing this right? Below are the codes:

public Boolean AddToDigitalAssets(string fileName)
{
    ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
    Boolean result = true;
    using (SqlConnection cnn = new SqlConnection(_ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
             using (SqlDataAdapter da = new SqlDataAdapter(cmd))
             {

             }
        }
    }
    return result;
}
Was it helpful?

Solution

Yes it is perfectly Valid and Good Practice.

you can even remove the curley braces for first using block as below:

using (SqlConnection cnn = new SqlConnection(_ConnectionString))            
using (SqlCommand cmd = new SqlCommand())
{

}

OTHER TIPS

the using block "guarantees disposal of the resource... even in the case of an unhandled exception."

Take that "guarantee" with a grain of salt. Many things can prevent the disposal of the resource. What if the using block contains an infinite loop? Or the block throws an exception, a hostile exception filter higher on the stack goes into an infinite loop and never returns control to the finally block associated with the using statement? Or the block calls Environment.FailFast? There are many, many things that can prevent the disposal from ever running. Never write a program that relies on disposal for its correctness. Disposal is there for politeness, to return scarce resources to the pool for others to use them.

Moreover, let me make sure this point is clear: truly unhandled exceptions are implementation-defined behavior in C#. The using block's finally clause is there to deal with the situation where an exception is thrown inside the using block and then handled somewhere else, not to deal with the situation where an exception is thrown inside the block and never handled. If that happens then it is entirely up to the implementation to determine what happens; the C# language makes a total of zero promises about the behavior of a program that throws an exception that is never handled. The resources might be disposed. They might not. You're in a building that is about to be unexpectedly demolished; do you really want to spend time washing the dishes and putting them away neatly?

Is there any benefit to multiple nested Using blocks?

Yes.

Does a single Using block already guarantee that all of the resources it contains will be disposed?

No. Only the resources that are actually mentioned by a using statement are cleaned up. That's why you nest them.

There are a few cases where technically you don't have to because the inner one takes responsibility for releasing the same resource as the outer one. But it doesn't hurt anything to nest using blocks and it makes it very clear to the reader what is going on. The best practice here is to have one using statement for every resource that you want cleaned up.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top