The using block can be applied to any .NET object that implements the IDisposable interface. If you try to apply a block to an object that does not, you will get a compilation error.
The using block actually generates a try-finally block around the code in the using block.
Following:
using (con) { con.Open(); cmd.ExecuteNonQuery(); }
equivalent to writing:
try { con.Open(); cmd.ExecuteNonQuery(); } finally { con.Close(); con.Dispose(); }
Using using is a performance enhancement that provides the correct cleanup of a one-time object and, in my opinion, creates code that is easier to read and maintain.
Note. You can nest using blocks, for example:
using(con) using(cmd) { }
The using statement in C # is the equivalent of the Imports statement in VB.NET.
source share