Csharp Code Samples

Rohan’s Blog – C# .NET Programming
  • rss
  • Home
  • About
  • Search
  • Contact Me

The Using Block

Rohan Warang | March 21, 2009

The using block provides proper declaration and dispose of objects that implement IDisposable interface. IDisposable is implemented by managed classes that access unmanaged resources. FileStream is one such classes that accesses IntPtr and SafeFileHandle which are unmanaged handles.

Finally Block

The problem with unmanaged resources is that they can throw exceptions which are beyond the control of the programmer.

For example a FileNotFoundException is thrown if the file being access by a FileStream does not exist. These exceptions need to be handled by the code by defining try catch blocks. And in the end a finally block needs to be defined for closing and disposing the FileStream.

Show Code

FileStream file = null;
try
{
    file = new FileStream("C:\\input.txt", FileMode.Open);
    // Read the file
}
catch
{
    throw;  // Handle the exception or throw it
}
finally
{
    if (file != null)
    {
        file.Close();
        file.Dispose();
    }
}

The Close() method is called for temporarily closes the resource used by the object they can be reopened again; whereas Dispose() completely releases all the resources.

Using Block

In the above code if you notice in the finally block the file is first checked if it is null only then close() and dispose() can be called. The reason is that if an exception occurs even before instantiating the file object then it will throw an exception again in the finally block when we try to close it.

To avoid these handling issues and have a more cleaner code we use the using block as shown below.

Show Code

try
{
    using (FileStream file = new FileStream("C:\\input.txt", FileMode.Open))
    {
        // Read the file
    }
}
catch
{
    throw; // Handle the exception or throw it
}

Note that the scope of file object is limited to the braces only { … } and not beyond that.


Categories
Tutorials
Tags
Try Catch, Using
Comments rss
Comments rss
Trackback
Trackback

« Linq Filters On Lists Asp.net Permalinks Using URL Rewriting »

4 Responses to “The Using Block”

  1. binary says:
    March 21, 2009 at 8:42 pm

    thx, now it is more clear for me. I just wondering for what exactly should I look in Microsoft documentation to know for which classes should I use Using statement?

  2. Rohan Warang says:
    March 21, 2009 at 8:45 pm

    @binary
    Your always welcome

  3. kumar says:
    April 29, 2009 at 2:10 pm

    i need to ask what is role of .net memory management

  4. Rohan Warang says:
    April 30, 2009 at 12:26 am

    @kumar
    The Dispose() method frees the unmanaged resources accessed by the current object. Alternatively you can also use Dispose() to free managed resource references in your class.
    A garbage collector in .net memory management checks for such objects that are not being referred from any memory location and cleans them up from the managed heap (makes the location available to newer objects).
    I hope this answers your question.

Leave a Reply

Click here to cancel reply.

Spam protection by WP Captcha-Free

Subscribe

dZone

Categories

  • Optimization
  • Tutorials

Admin

  • Log in
Creative Commons License rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox