Click here to Skip to main content
12,752,319 members (34,447 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


13 bookmarked
Posted 11 Oct 2010

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

, 12 Oct 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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:

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


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


About the Author

Dinesh K Mandal
Technical Lead Imfinity India Pte Ltd, Noida (Excelsoft Company)
India India

You may also be interested in...


Comments and Discussions

Generalreason for a 3 vote Pin
Simon_Whale8-Nov-10 2:16
memberSimon_Whale8-Nov-10 2:16 
GeneralMy vote of 3 Pin
prerakpatel19-Oct-10 19:23
memberprerakpatel19-Oct-10 19:23 
GeneralMy vote of 1 Pin
ignatandrei18-Oct-10 8:58
memberignatandrei18-Oct-10 8:58 
GeneralRe: My vote of 1 Pin
mcconville@xenomorph18-Oct-10 23:02
membermcconville@xenomorph18-Oct-10 23:02 
GeneralRe: My vote of 1 Pin
ignatandrei18-Oct-10 23:35
memberignatandrei18-Oct-10 23: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 12 Oct 2010
Article Copyright 2010 by Dinesh K Mandal
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid