Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here



Google

Understand "using" Statement of C# in detail with simple,nested and staked style of implementation


Share with WhatsApp


Understand using Statement of C# in detail with simple,nested and staked style of implementation


In this post I am going to share information about my recent testing about "using" statement in C#  
which is very common in use now.

Most of the developers know that when we use a using statement, compiler converts it to try and finally block which ensures the correct use of IDisposable objects. In one of my code I was using a nested using statement while which I was thinking about how compiler is going to translate it and this made me to test it using IL Disassembler so I have tested it with different implementations and here is what I came to know.

Before reading further if you are not so aware about using statement then I will recommend reading MSDN topic about using statement here.

First for simple using statement.

public void SingleUsingStatement()
{
    using (DiposableClass obj = new DiposableClass())
    {
        //work with obj.
    }

}

And below is the IL code screenshot.

IL code of using Statement of C# in simple implementation

If you see above screenshot of IL code of simple implementation of using statement, IL converts it to try-finally block.

Then I have tested it for nested using statement like below.

 public void NestedUsingStatements()
{
    using (DiposableClass obj1 = new DiposableClass())
    {
         //work with obj1.
         using (DiposableAnotherClass obj2 = new DiposableAnotherClass())
         {
            //work with obj2
         }
    }
}

IL code screen shot for above code

IL code of using Statement of C# in nested,stacked and same type in single style of implementation

And for stacked style of declaring different types in using statements
 
 

public void StackedAnddDifferentUsingStatements()
{
    using (DiposableClass obj1 = new DiposableClass())
    using (DiposableAnotherClass obj2 = new DiposableAnotherClass())
    {
          //work with obj1.              
          //work with obj2
    }
}

 

IL code screen shot for above code

IL code of using Statement of C# in nested,stacked and same type in single style of implementation

And have also checked for same types declaration in single using like below...

public void SameTypesInUsingStatements()
{
   using (DiposableClass parentDiposableClass = new DiposableClass(), childDiposableClass = new ChildDiposableClass())
   {
      //work with ChildDiposableClass.              
      //work with childDiposableClass
   }
}

IL code screen shot for above code

IL code of using Statement of C# in nested,stacked and same type in single style of implementation

If you comapred all avobe IL codes other than simple implementation you will find it almost same i.e. compiler translated code in to nested try - finally block.

Hope you have liked this simple post. Share your thoughts in comment section below.

Do also share if you too are curious about how compiler understands a specific code.

Thanks for reading. Share this with your friends. :-)



If you enjoyed this post take 5 seconds to share it! Be Socialable. :-)

Share with WhatsApp

Posts To Read Next

Typewriter - Automatic TypeScript Template generation from C# classes in Visual Studio

Typewriter, an extension for visual studio that generates TypeScript files from C# code files using TypeScript templates. Convert your C# model classes to TypeScript automatically.


How to call click or any event only once in jQuery

Know how to execute an click event or any event only once for any element in jQuery. Perform action only once and even not required to unbind event.


Top 10 Visual Studio things which can boost developers coding speed

Visual Studio 2012 provides some coding features by which you can code faster if use them properly. This post will cover top 10 things among them to boost your development speed.


Copy changed data in table from one database to another in SQL stored procedure

How to copy or merge changed data in table from one database to another having same schema is the topic of this post.


Swagger - Powerful Opensource Framework for RESTful API

With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability. Swagger Editor allows you to generate code, documentation with ease.


Your opinion is valuable for us! Comments, suggetions are welcome.


Submit your Email Id to stay updated with us and get notified with our new posts. It's FREE!
Vu 32'' TV Vu 24'' TV
We know this popup is disturbing you!
But We would greatly appreciate if you share us with your friends below!

It will not take more than 2 seconds but will motivate us greatly to write more,share more!

x