Using the using statement in C# - A world apart from the everday ...

A world apart from the everday ...

Assert.IsTrue(Entries.Count == 0);

Using the using statement in C#

I am sure we all know the use of the using statement in C# so I won't go into very much detail about it as that is not the point of this blog entry;
 
If you are unsure about the statement or it's purpose then go checkout MSDN library for a detailed reference @ using Statement (C# Programmer's Reference)
 
So now that we are all on the same page regarding the use of the statement I would like to address a usability issue regarding this particular gem ...
 
Consider this example;
 
using (ObjectX x = new ObjectX)
{
         using (ObjectY y = new ObjectY)
         {
                 using (ObjectZ z = new ObjectZ)
                {         
                         //TODO: some implementation should go here
                }
         }
}
 
I have often needed to nest using statements like this which after a couple of nestings makes your code rather unreadable.
 
I discovered (purely by accident and was then backed up by a blog entry on Eric Gunnerson's C# Compendium site) that you could do the following instead;
 
   using (ObjectX x = new ObjectX())
   using (ObjectY y = new ObjectY())
   using (ObjectZ z = new ObjectZ())
   {
            //TODO: some implementation should go here
   }
 
Which to me is far more readable than the initial code snippet!
 
What do you guys think?
Does this make the code easier, or (heaven forbid) more difficult to read?
Posted: Feb 27 2006, 05:22 PM by Ryan CrawCour | with 4 comment(s)
Filed under:

Comments

barend said:

I think that if you have a big code block after all the usings, you might want to add a comment at the end of the block as to which usings were used.  That should remove any readability problems.

I think it looks much cleaner than the nested approach.
# February 28, 2006 9:38 AM

Simon Stewart said:

I'd vote for the first block - the second one isn't very readable.

btw, if you are want to 'use' multiple instances of the same type you can use the following syntax:

           using(Person x= new Person(),
                    y = new Person(), z = new Person())
           {
           }
# February 28, 2006 11:07 AM

Steve Crane said:

The second method is possible because each using (and commands nested within it is) a single compound statement, so can be nested without need for braces.  But I think it would be clearer to code it as

  using (ObjectX x = new ObjectX())
     using (ObjectY y = new ObjectY())
        using (ObjectZ z = new ObjectZ())
        {
                 //TODO: some implementation should go here
        }
# February 28, 2006 12:01 PM

Ryan CrawCour said:

Steve,

That's an interesting implementation but it does not accomplish anything over option 1 I listed in the original post. You will still have multiple levels of identation which means sooner or later your code will become quite unreadable.

This brings up another point though; should your code ever have so many indentation levels that it becomes unreadable? perhaps if your code gets to this point it is time for a serious refactoring or rethinking!

Anyways, thanks for the comments ...
# February 28, 2006 12:10 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: