none
What is Difference between line declaration and using statement RRS feed

  • Question

  • I wonder what is difference between these two codes: 

     using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(query, DBAccess.Connect()))

                {

                    sqlDataAdapter.Fill(dt);

                }

    and

     

    new SqlDataAdapter(query, DBAccess.Connect()).Fill(dt);

     

    aren't those two are same? and how do you call the second line? is it "shallow object" or what?

    Thursday, December 8, 2011 8:23 AM

Answers

  • The using statement calls the Dispose method on the object in the correct way, and it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.
    The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler. 
    Thursday, December 8, 2011 8:47 AM

All replies

  • The using statement calls the Dispose method on the object in the correct way, and it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.
    The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler. 
    Thursday, December 8, 2011 8:47 AM
  • Hi Murati,

    if you write code using the keyword "using" the object created (i.e. sqlDataAdapter) will go out of scope as sson as using block is executed. The memory space used by the variable is ready to be disposed. In other words, object is ready to be garbaged.

    Using "Using" is a good practice as it helps the garbage collector to collect and clean the garbage.But the type that you are using must inherit from IDisposable.

     

    Whereas if you simply create an object using "new" keyword the garbage collector will not get to know the scope of that object immediately. It will stay longer in memory thus memory allocation/resource utilization will be higher.

     

    I hope it helps.

    For further refer:

    http://msdn.microsoft.com/en-us/library/yh598w02%28v=vs.80%29.aspx

    http://msdn.microsoft.com/en-us/library/51y09td4%28v=vs.71%29.aspx

     


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful"
    Thursday, December 8, 2011 8:49 AM
  • using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(query, DBAccess.Connect()))
    {
        sqlDataAdapter.Fill(dt);
    }
    

    The above is translated by the compiler as try-finally block and you can use the sqlDataAdapter variable multiple times inside the block for further operations:

     

    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(query, DBAccess.Connect());
    try
    {
        sqlDataAdapter.Fill(dt);
    }
    finally
    {
        sqlDataAdapter.Dispose();
    }
    


    In case of the following code, an anonymous object is created which can be used only once because you loose reference to it after the statement. The other downside is that you will not be able to call Dispose() on the object:

    new SqlDataAdapter(query, DBAccess.Connect()).Fill(dt);
    

     

     


    Click the 'Vote as Helpful' arrow if this post was helpful.
    Thursday, December 8, 2011 12:05 PM
  • Hi Murati,

    We have not heard from you in a couple of days.

    Please post back at your convenience if we can assist further.

    Enjoy your day!


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Monday, December 12, 2011 2:14 AM
    Moderator