views:

180

answers:

2

According to MSDN Library

using Statement (C# Reference)
Defines a scope, outside of which an object or objects will be disposed.

But I got this code posted here by some user and I got confused about this: (please see my comment on the code)

using (OleDBConnection connection = new OleDBConnection(connectiongString))
           {
                if (connection.State != ConnectionState.Open)
                    connection.Open();
                string sql = "INSERT INTO Student (Id, Name) VALUES (@idParameter, @nameParameter)";

                using (OleDBCommand command = connection.CreateCommand())
                {
                    command.CommandText = sql;
                    command.CommandType = CommandType.Text;

                    OleDBParameter idParameter = command.CreateParameter();
                    idParameter.DbType = System.Int32;
                    idParameter.Direction = Parameterdirection.Input;
                    idParameter.Name = "@idParameter";
                    idParameter.Value = studentId; 

                    OleDBParameter nameParameter = command.CreateParameter();
                    try
                    {
                        command.ExecuteNonQuery();
                    }
                    finally
                    {
                        // Is it still necessary to dispose these objects here?
                        command.Dispose();
                        connection.Dispose();
                    }
                }
            }

In the above code, does the using statement properly used? I'm confused, can anyone please explain how to use using statement and its scoping and when, where and why to use it. Thank you..

+12  A: 

The finally block (and thus in this case the try) is redundant, that's what using does, it calls Dispose on the IDisposable object with which it is initialized when the using block ends (regardless of exceptions or lack thereof).

Motti
+8  A: 

The using statement is a shorthand for manually putting a try/finally block.

So

using( x ){
  ...
}

Is the same as

try{
  ...
}finally{
  if( x != null ){ x.Dispose(); }
}

And they will generate the same IL when compiled.

The compiler will give you an error in case x does not implement IDisposable.

Øyvind Bråthen
actually same to if(x != null){ ((IDisposable)x).Dispose(); } - using Statement (C# Reference) http://bit.ly/acmoOD
Nick Martyshchenko
I find it sad that just because I posted my answer one minute before you I have (currently) twice the votes, this doesn't bode well for SO, your question isn't inferior to mine in any way but because you didn't draw fast enough it (probably) won't rise above mine.
Motti
That's the way SO works at the moment I guess, and I'm not sure if there are a quick "fix" for it either. Sadly, it promotes making quick answers instead of well thought out ones. I find myself often writing a quick answer even if I know I'm going to need to edit it later on to not be completly sunk. I have thought about this for a while, and one suggestion I have is that after a question is posted, no answers will be shown for 10 minutes, and then after 10 minutes all the answers written in this timeframe is shown in a random order with the same timestamp. Might be a good idea ;)
Øyvind Bråthen
That might shift the focus to actually write a good enough answer in those 10 minutes so that it will be better than the other ones when the answers are shown to the public, instead of just getting it "out there" as soon as possible regardless of content. I think your answer on this questions is also a good one, so by that virtue I think we should have around the same amount of votes. I hope the SO staff makes some measures to fix these issues after they think it through.
Øyvind Bråthen