Click here to Skip to main content
15,177,139 members
Articles / Programming Languages / C#
Article
Posted 29 Mar 2004

Stats

576.5K views
102 bookmarked

Understanding the 'using' statement in C#

Rate me:
Please Sign up or sign in to vote.
4.71/5 (106 votes)
29 Mar 2004CPOL2 min read
Understanding the 'using' statement in C#

Introduction

This article is an introduction to the using statement in c# and also provides some insight into the actual implementation of the statement.

The Code

When you are using an object that encapsulates any resource, you have to make sure that when you are done with the object, the object's Dispose method is called. This can be done more easily using the using statement in C#. The using statement simplifies the code that you have to write to create and then finally clean up the object. The using statement obtains the resource specified, executes the statements and finally calls the Dispose method of the object to clean up the object. The following piece of code illustrates its use.

C#
using (TextWriter w = File.CreateText("log.txt"))
{
    w.WriteLine("This is line one");
}

Now that's cool. But before we can start using this code, let us try to understand what happens behind the screen. Lets have a look at the IL code for the above code section.

MSIL
.locals init ([0] class [mscorlib]System.IO.TextWriter w)
  IL_0000:  ldstr      "log.txt"
  IL_0005:  call       class [mscorlib]System.IO.StreamWriter 
      [mscorlib]System.IO.File::CreateText(string)
  IL_000a:  stloc.0
 
 .try
  {
    IL_000b:  ldloc.0
    IL_000c:  ldstr      "This is line one"
    IL_0011:  callvirt   instance void [mscorlib]
      System.IO.TextWriter::WriteLine(string)
    IL_0016:  leave.s    IL_0022
  }  // end .try
  finally
  {
    IL_0018:  ldloc.0
    IL_0019:  brfalse.s  IL_0021
    IL_001b:  ldloc.0
    IL_001c:  callvirt   instance void [mscorlib]
      System.IDisposable::Dispose()
    IL_0021:  endfinally
  }  // end handler

Hmmmm.... Well doesn't look like this is my code. That's because I see a try and a finally in the IL code (something that I haven't implemented). Wait a minute. IT IS MY CODE....

Waaaaaah... Somebody changed my code...

Well the truth is, somebody did change your code. The CLR. The CLR converts your code into MSIL. And the using statement gets translated into a try and finally block. This is how the using statement is represented in IL. A using statement is translated into three parts: acquisition, usage, and disposal. The resource is first acquired, then the usage is enclosed in a try statement with a finally clause. The object then gets disposed in the finally clause. For example the following lines of code using the using statement,

C#
using (MyResource myRes = new MyResource())
{
    myRes.DoSomething();

}

gets translated to,

C#
MyResource myRes= new MyResource();
try
{
    myRes.DoSomething();
}
finally
{
    // Check for a null resource.
    if (myRes!= null)
        // Call the object's Dispose method.
        ((IDisposable)myRes).Dispose();
}

Hmmm... That explains it.

The above code that uses the using statement corresponds to one of the two possible expansions. When MyResource is a value type, the expansion in the finally block will be

finally<BR>{<BR>((IDisposable)myRes).Dispose();<BR>}<BR>

If MyResource is of reference type, the expansion becomes

finally<BR>{<BR>if(myRes != null)<BR>((IDisposable)myRes).Dispose();<BR>}

This way, if a null resource is acquired, then no call will be made to Dispose, thus avoiding any exception that occurs.
Well, that explains everything.

Using 'using'

A typical scenario where we could use the using statement is :

C#
string connString = "Data Source=localhost;Integrated " + 
  "Security=SSPI;Initial Catalog=Northwind;";

using (SqlConnection conn = new SqlConnection(connString))
{
  SqlCommand cmd = conn.CreateCommand();
  cmd.CommandText = "SELECT ID, Name FROM Customers";
  
  conn.Open();

  using (SqlDataReader dr = cmd.ExecuteReader())
  {
    while (dr.Read())
      Console.WriteLine("{0}\t{1}", dr.GetString(0), dr.GetString(1));
  }
}

Note

The using statement is only useful for objects with a lifetime that does not extend beyond the method in which the objects are constructed. Remember that the objects you instantiate must implement the System.IDisposable interface.

There is no equivalent for the using statement in vb.net. You have to use the try finally block.

License

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

Share

About the Author

Tingu Abraham
Canada Canada
No Biography provided

Comments and Discussions

 
Generalreturn statement Pin
simonemaynard20-Nov-07 10:26
Membersimonemaynard20-Nov-07 10:26 
GeneralRe: return statement Pin
Member 404802016-Jul-08 2:31
MemberMember 404802016-Jul-08 2:31 
Question.NET 2.0 Pin
MAC09107412-Sep-07 1:15
MemberMAC09107412-Sep-07 1:15 
AnswerRe: .NET 2.0 Pin
blackjack215013-Jul-08 21:38
Memberblackjack215013-Jul-08 21:38 
QuestionGFDL license Pin
Kevin Whitefoot10-Jan-06 1:18
MemberKevin Whitefoot10-Jan-06 1:18 
Generalusing statement Pin
Balamurali Balaji16-May-05 20:40
MemberBalamurali Balaji16-May-05 20:40 
GeneralThere is no Catch! Pin
Ed K30-Mar-04 5:23
MemberEd K30-Mar-04 5:23 
GeneralRe: There is no Catch! Pin
Ian Griffiths31-Mar-04 23:40
MemberIan Griffiths31-Mar-04 23:40 
Actually that's part of the point of the using statement!

The idea behind finally blocks (and therefore, by extension, the using statment) is that they let you write cleanup code without catching the error. And it's a fairly common style of programming to have a lot more code dedicated to cleaning up than to handling errors. For example, if a non-recoverable error occurs anywhere during the processing of a web page, you're likely to be doing the same thing in every case: returning a 500, generating some kind of error page, and doing whatever diagnostic logging/alterting you need do. So you'll probably have one catch handler quite high up in the scheme of things. (In fact you might even just use the built-in infrastructure of ASP.NET to catch the error.) But you'll probably have *lots* of tidying up to do.

So in some applications it's not uncommon to have an order of magnitude more finally blocks than catch blocks.

Also, consider what happens when you're writing code that needs to throw an exception to the caller. You will want to make sure that your code frees any resources it created prior to throwing the exception, but it certainly wouldn't make sense to catch the exception in the same function you threw it in. The behaviour you want is for all your cleanup code to run and for the exception to be thrown to the caller.

In situations where it's appropriate to catch the error, you just use a try catch block. But there are plenty of scenarios where you want to tidy up and then let the exception flow up to the caller.

--
Ian Griffiths
DevelopMentor
Generalgreat article Pin
martininick30-Mar-04 4:06
Membermartininick30-Mar-04 4:06 
Generalsealed Pin
Jonathan de Halleux30-Mar-04 3:35
MemberJonathan de Halleux30-Mar-04 3:35 
GeneralRe: sealed Pin
Maximilian Hänel30-Mar-04 5:36
MemberMaximilian Hänel30-Mar-04 5:36 

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.