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).

11
  • It certainly works. If it's OK is between you and your coding style. Commented Jan 31, 2013 at 16:48
  • Yes, it's completely correct :) Commented Jan 31, 2013 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 { } Commented Jan 31, 2013 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 Commented Jan 31, 2013 at 16:49
  • 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. Commented Jan 31, 2013 at 16:50

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This is also true for any structured statement like if, while and so on.
@OlivierJacot-Descombes Except try/catch. Here's why
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.

1 Comment

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.
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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.