- The using block will dispose of the OdbcConnection.
- Normal scope rules work for anything declared inside the using block.
- The using block will not clean up any other IDisposable objects. It only cleans up the declared item
- note that you can nest using blocks, or if the items are the same type, multiple items can be initialized at the same time.
See the top bit of my other answer for How do I use the using keyword in C# for a little more information.
I should also mention that you can close (dispose) of the connection as soon as you are done with it to release the resource. The guidelines say that the caller should be able to repeatedly call the dispose method. The using block is essentially just a safety net and allows writing clearer code in most circumstances.
[Edit]
e.g. multiple initialization in a using: initialize more than one object in the same using without having to nest using blocks if the objects are the same type:
using (Bitmap b1 = new Bitmap("file1"), b2 = new Bitmap("file2"))
{ ... }
Joel Coehoorn mentioned stacking, which is nesting but omitting the braces, much as you can omit the braces in a for
, or if
statement. The UI doesn't reformat with an indent. I'd be curious what the IL looks like.
using(Bitmap b = new Bitmap("filex"))
using(Graphics g = Graphics.FromImage(b))
{
}
It is an error to use put different objects in the same using error CS1044: Cannot use more than one type in a for, using, fixed, or declaration statement.
// error CS1044
using(Bitmap b = new Bitmap("filex"), Graphics g = Graphics.FromImage(b))