Is it possible to use `using` like this?

Possible duplicate:
Nested statements in C #

I am a big fan of using instructions in C #. I find this:

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

... very readable than this:

 var foo = new ObjectiWhichMustBeDisposed(); try { other code } finally { foo.Dispose(); } 

This is not only more readable, but also prevents the random use of the foo variable after the using statement (i.e., after deleting it), whereas in the second example, foo could be used after it was placed.

One of the problems with using is that it leads to very nested code if many one-time objects are created. For instance:

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

If both objects are of the same type, you can combine them into one using statement, for example:

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

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

My question is whether a similar end can be achieved as follows:

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

In this case, after the first use, there are no curly braces (and, therefore, there is no need to backtrack from the code). This compiles, and I assume that it works the same as an if without curly braces, i.e. It will use the following status (the second using in this case) as its "body".

Can anyone confirm if this is correct? (the description of the operator used does not help).

+6
source share
4 answers

Look at the definition of the using statement in the C # standard:

12.3.3.17 Using statements
To use the stmt operator of a form:
using the built-in operator

embedded-statement is that of the following (see clause A2.5):

submerged statement:
block
empty expression
operator expression
Operator selection
iteration operator
jump-expression
fitting rooms expression
registered expression
unregistered-expression
Expression Lock
using-expression
yield-statement

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

+2
source

Yes, the code you offer

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

and normal, and quite often.

You do not need to use extra {} after each use (different from the one that contains another code, if the other code contains more than one statement), because each of them 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 } 

will also be good.

+7
source

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 encoding method, go with it.

+1
source

Yes this is correct; however, this may not be a very good coding style for you.

There was a lot of discussion regarding SO regarding this: Why is it considered bad practice to skip braces? The OP post specifically mentions the use of assertions. Perhaps worth a read.

+1
source

All Articles