Click here to Skip to main content
15,892,161 members
Articles / Programming Languages / Visual Basic

Using Statement and Dispose Method in C# and VB.NET

Rate me:
Please Sign up or sign in to vote.
3.40/5 (5 votes)
12 Oct 2010CPOL 58.4K   13   5
Using Statement and Dispose Method in C# and VB.NET

Since .NET 2.0, we are aware of using statement to perform resource intensive operations like database operations, file IO operations, etc. Using statement basically marks a boundary for the objects specified inside using () braces. So when code block using (){} (in C#) or Using End Using (in VB.NET) is exited either after normal execution or some exception cause, the framework invokes the Dispose method of these objects automatically. Before creating or using any object inside using block, one must make sure that the object type implements the IDisposable interface.

We can specify multiple objects inside using-block, and also write using-blocks in stack as shown in the example below.

C#
public class DotNetTips     
{     
private void DoSomeOperation()     
{     
using (SqlConnection con1 = new SqlConnection("First Connection String"), 
			con2 = new SqlConnection(("Second Connection String"))     
{     
// Rest of the code goes here     
}     
}

private void DoSomeOtherOperation()    
{     
using (SqlConnection con = new SqlConnection("Connection String"))     
using (SqlCommand cmd = new SqlCommand())     
{     
// Rest of the code goes here     
}     
}     
}

Using statement is useful when we have to call dispose method multiple times on different objects. Otherwise, we will have to call Dispose method on each object as:

C#
if (con != null) con.Dispose();     
if (cmd != null) cmd.Dispose();  

When the using block code compiles to intermediate language, it translates the using statements into equivalent try/finally block.

I hope this tip can be useful for all.


Posted in .NET Technologies, C#/VB.NET, CodeProject, Dot NET Tips Tagged: .NET 3.5, C#, Dispose, Using Statement

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Imfinity India Pte Ltd, Noida (Excelsoft Company)
India India
http://www.imfinity.com/

Comments and Discussions

 
Generalreason for a 3 vote Pin
Simon_Whale8-Nov-10 1:16
Simon_Whale8-Nov-10 1:16 
GeneralMy vote of 3 Pin
Prerak Patel19-Oct-10 18:23
professionalPrerak Patel19-Oct-10 18:23 
GeneralMy vote of 1 Pin
ignatandrei18-Oct-10 7:58
professionalignatandrei18-Oct-10 7:58 
The usual things that you can read - and not even a good written piece
GeneralRe: My vote of 1 Pin
James McConville18-Oct-10 22:02
James McConville18-Oct-10 22:02 
GeneralRe: My vote of 1 Pin
ignatandrei18-Oct-10 22:35
professionalignatandrei18-Oct-10 22:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.