Using Statement in C#

You have probably used using statement a lot,here I want to write about how it has been implemented.

In the using statement,you initialize an object and save its reference in a variable . Then you access the variable via code contained inside using's braces.When you compile the code , the compiler automatically emits the try and finally blocks.

Inside the finally block,the compiler emits code to cast the object to an IDisposable and calls the Dispose method.

Obviously,the compiler allows the using statement to be used only with types that implement the IDisposable interface.

The using statement also works with value types that implement the IDisposable interface.This allows you to create an extremely efficient and useful mechanism to encapsulate the code necessary to begin and end an operation.

Here is one more article on using statement with sample code:

http://www.c-sharpcorner.com/UploadFile/DipalChoksi/UsingStatement11092005065819AM/UsingStatement.aspx

Up Next
    Ebook Download
    View all
    Learn
    View all