6

Possible Duplicate:
Nested using statements in C#

I'm a big fan of the using statement in C#. I find this:

using (var foo = new ObjectWhichMustBeDisposed())
{
    other code
}

...very much more readable than this:

var foo = new ObjectiWhichMustBeDisposed();

try
{
    other code
}
finally
{
    foo.Dispose();
}

Not only is it more readable, it also prevents accidental use of the foo variable after the using statement (i.e. after it has been disposed), whereas in the second example foo could be used after it had been disposed.

One problem with using, though, is that it tends to lead to very nested code if lots of disposable objects are being created. For example:

using (var foo = new ObjectWhichMustBeDisposed())
{
    using (var bar = new ObjectWhichMustBeDisposed())
    {
        other code
    }
}

If both the objects are of the same type, then you can combine them into a single using statement, like so:

using (var foo = new ObjectWhichMustBeDisposed(),
           bar = new ObjectWhichMustBeDisposed())
{
    other code
}

However, if the objects are not of the same type, then this will not work.

My question is whether it is OK to to achieve a similar end like this:

using (var foo = new ObjectWhichMustBeDisposed())
using (var bar = new OtherObjectWhichMustBeDisposed())
{
    other code
}

In this case, there are no curly-braces after the first using (and hence no need to indent the code). This compiles, and I assume that this works just like an if statement with no braces - i.e. it'll use the next statment (the second using in this case) as its "body".

Can anyone confirm whether that's correct? (The description of the using statement is no help).

Community
  • 1
  • 1
Gary McGill
  • 26,400
  • 25
  • 118
  • 202
  • It certainly works. If it's OK is between you and your coding style. – CodesInChaos Jan 31 '13 at 16:48
  • Yes, it's completely correct :) – cuongle Jan 31 '13 at 16:48
  • It compiles fine the only thing that I would say is that for readability and consistency stick with a style of wrapping code blocks around `{ }` – MethodMan Jan 31 '13 at 16:49
  • Sorry I don't actually have an answer for you, just wanted to say I like this latter approach and use it a lot in order to reduce nesting, especially when using sql commands/connections etc – RobJohnson Jan 31 '13 at 16:49
  • I dont see problems with the indentation of several disposable objects. However I would make the code more readable using methods to encapsulate some behaviour. – gustavodidomenico Jan 31 '13 at 16:49
  • No matter if that's correct, I would say : *Be kind to the one who will maintain your code and keep the curly braces :p* – hoang Jan 31 '13 at 16:50
  • 4
    @hoang I consider it kinder to future readers to *omit* the curly braces. It makes the code *easier* to read, not harder, and is a common enough pattern that it shouldn't be too surprising. – Servy Jan 31 '13 at 16:50
  • As long as it's consistent throughout the application, omitting the curly braces is fine. I rarely wrap a single statement or block in curly braces myself. – JosephHirn Jan 31 '13 at 17:00
  • @Servy Have fun and put some line breaks and comments between using statements to see how readable it is :p – hoang Jan 31 '13 at 17:01
  • This is completely legitimate. See here: http://stackoverflow.com/questions/1329739/nested-using-statements-in-c-sharp the selected answer. – Aniket Inge Jan 31 '13 at 16:49
  • @hoang If line breaks and comments reduce readability, then either omit them or move them to a more suitable location in which they don't hamper readability. – Servy Jan 31 '13 at 18:16

4 Answers4

7

Yes the code you propose

using (var foo = new ObjectWhichMustBeDisposed())
using (var bar = new OtherObjectWhichMustBeDisposed())
{
    other code
}

is both OK and quite common.

You do not need to use additional { } after each using (other than the one that contains other code, if other code is more than one statement) because each using has exactly one statement following it.

using (var foo = new ObjectWhichMustBeDisposed())
using (var bar = new OtherObjectWhichMustBeDisposed())
using (var baz = new OtherObjectWhichMustBeDisposed())
using (var quux = new OtherObjectWhichMustBeDisposed())
{
    other code
}

would also be fine.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
2

Look at using statement definition in C# standard:

12.3.3.17 Using statements
For a using statement stmt of the form:
using ( resource-acquisition ) embedded-statement

embedded-statement is whatever from the following (See Item A2.5):

embedded-statement:
block
empty-statement
expression-statement
selection-statement
iteration-statement
jump-statement
try-statement
checked-statement
unchecked-statement
lock-statement
using-statement
yield-statement

So both usages of using (block or another using-statement) are absolutely equivalent from C# Standard point of view.

Alexander Stepaniuk
  • 6,217
  • 4
  • 31
  • 48
  • Thanks to everyone who responded, and sorry I can't accept you all, but I thought it better to accept this answer - which is definitive / backed up with evidence. – Gary McGill Jan 31 '13 at 18:12
1

Yes, this is correct.

using (var foo = new ObjectWhichMustBeDisposed())
using (var bar = new OtherObjectWhichMustBeDisposed())
{
    other code
}

So the rest is up to you. If you like this coding way, go with it.

Tigran
  • 61,654
  • 8
  • 86
  • 123
1

Yes it's correct; however it may not be good coding style for you.

There was a lengthy discussion on SO regarding this: Why is it considered a bad practice to omit curly braces? In the post the OP specifically mentions using statements. It may be worth the read.

Community
  • 1
  • 1
Robert H
  • 11,520
  • 18
  • 68
  • 110