- Posted On 11 April 2014
- By
- In Programming
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.
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
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
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
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. :-)
- Tags :
- CSharp
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.
Visual Studio 2008 Shell and TFS integration
Visual Studio 2008 Shell and TFS integration is the problem for all newbies of BIDS and TFS. Here is the solution.
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.
Assembla - Free and private repository to manage your source code online with SVN subversion hosting
With Assembla you can share source code with others online. Free & Private source code repository with SVN Subversion, Git & Perforce Hosting.
Best CSS Gradient background generator tools online
Here are some best CSS gradient background code generator online tools using which you can create a cross browser css code for gradient backgrounds.