Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How write several using instructions? [duplicate]

Tags:

Possible Duplicate:
using statement with multiple variables

I have several disposable object to manage. The CA2000 rule ask me to dispose all my object before exiting the scope. I don't like to use the .Dispose() method if I can use the using clause. In my specific method I should write many using in using:

using (Person person = new Person()) {     using (Adress address = new Address()) {          // my code     } } 

Is it possible to write this on another way like:

using (Person person = new Person(); Adress address = new Address()) 
like image 799
Bastien Vandamme Avatar asked Dec 13 '12 15:12

Bastien Vandamme


People also ask

What is an example of duplicate?

Adjective I began receiving duplicate copies of the magazine every month. I had a duplicate key made.

How do you manage duplicate records?

To remove duplicate values, click Data > Data Tools > Remove Duplicates. To highlight unique or duplicate values, use the Conditional Formatting command in the Style group on the Home tab.

How do I highlight more than 3 duplicates in Excel?

On the Home tab, in the Styles group, click Conditional Formatting > Highlight Cells Rules > Duplicate Values… The Duplicate Values dialog window will open with the Light Red Fill and Dark Red Text format selected by default. To apply the default format, simply click OK.


2 Answers

You can declare two or more objects in a using statement (separated by commas). The downside is that they have to be the same type.

Legal:

using (Person joe = new Person(), bob = new Person()) 

Illegal:

using (Person joe = new Person(), Address home = new Address()) 

The best you can do is nest the using statements.

using (Person joe = new Person()) using (Address home = new Address()) {   // snip } 
like image 194
Jim Dagg Avatar answered Jan 08 '23 04:01

Jim Dagg


The best you can do is:

using (Person person = new Person()) using (Address address = new Address()) {      // my code } 
like image 34
Z.D. Avatar answered Jan 08 '23 02:01

Z.D.