tags:

views:

278

answers:

7
using (Font font3 = new Font("Arial", 10.0f), 
           font4 = new Font("Arial", 10.0f))
{
    // Use font3 and font4.
}

I know that multiple objects of same type can be used inside a using clause.

Cant i use different types of objects inside the using clause?

Well i tried but although they were different names and different objects, they acted the same = had the same set of methods

Is there any other way to use the using class with different types?

If not, what is the most appropriate way to use it?

+25  A: 
using(Font f1 = new Font("Arial",10.0f))
using (Font f2 = new Font("Arial", 10.0f))
using (Stream s = new MemoryStream())
{

}

Like so?

Jesper Palm
+11  A: 

No you can't do it this way, but you can nest the using blocks.

using (Font font3 = new Font("Arial", 10.0f))
{ 
    using (Font font4 = new Font("Arial", 10.0f))
    {
        // Use font3 and font4.
    }
}

or as others said, but I'd not recommend it that way because of readability.

using(Font font3 = new Font("Arial", 10.0f))
using(Font font4 = new Font("Arial", 10.0f))
{
    // use font3 and font4
}
this. __curious_geek
to be honest - I find the latter more readable. If you are initialising three or four items (such as stream, streamreader, stream, streamwriter), the nesting can get totally out of control! I guess it is probably what you are used to.
Rob Levine
This completely depends on an individual. You use it the way you like or prefer.
this. __curious_geek
Though it should go without saying, consistency is the most important thing here.
etc
I would recommend the first one if you care about order of disposal and the second one in the normal case where it doesn't matter. I couldn't find any reference that explicitly said the order of disposal, the first one make the order of disposal unambiguous.
Dolphin
The order of disposal is same in both the cases. font3 will get disposed prior to font4 in both the cases.
this. __curious_geek
@curious: You're right that both have the same order of disposal, but font4 will be disposed first.
Ben Voigt
Yes ben, you're right. font4 will be disposed first.
this. __curious_geek
+3  A: 

You can only have a single type of object initialized in each using block. You can nest those as much as you want, however:

using (Font font3 = new Font("Arial", 10.0f))
{
    using (Brush b4 = new Brush())
    {

    }
}
Oded
Actually, you can have multiple objects of the same type, as the question shows.
Codesleuth
+2  A: 

You can nest them:

using (Font font3 = new Font("Arial", 10.0f))
using (font4 = new Font("Arial", 10.0f))
{
    // Use font3 and font4.
}

They should dispose in reverse order (font4 first).

EDIT:

This is exactly the same as:

using (Font font3 = new Font("Arial", 10.0f))
{
    using (font4 = new Font("Arial", 10.0f))
    {
        // Use font3 and font4.
    }
}
pm_2
Do you have a link to documentation which says they dispose in reverse order?
Dolphin
I think I heard it on a course, however, stacking them is the same as nesting them. See amended text.
pm_2
It is readily apparent from the grammar of C-like languages. The syntax of a *using-statement* is: using (*variable initialization*) *statement* Note that neither stacking nor braces are explicitly part of the syntax. They are implied by the recursive grammar. When *statement* is a using-statement, you get stacking. When *statement* is a *block-statement*, you have braces.
Ben Voigt
+5  A: 

You can stack using statements to accomplish this:

using(Font font3 = new Font("Arial", 10.0f))
using(Font font4 = new Font("Arial", 10.0f))
{
    // use font3 and font4
}
Secret Agent Man
+3  A: 

You can comma-delimit items of the same type - well, all I know is the compiler doesn't complain. You can also stack using () statements (use one set of brackets {}) of different types.

http://adamhouldsworth.blogspot.com/2010/02/things-you-dont-know.html

Adam
I've made a comment on your blog regarding the foreach example following your link.
Ed Courtenay
Good spot, I've changed it - it looked like it was occurring, but all that was happening was the compiler letting you ignore the brackets {}, it was just a nested foreach.
Adam
+5  A: 

The using statement purpose is to guarantee that the acquired resources are explicitly disposed by a call to Dispose method provided by the IDisposable interface. The specification does not allow you to acquire resources of different types inside a single using statement but having the first sentence in mind you can write this perfectly valid code in terms of the compiler.

using (IDisposable d1 = new Font("Arial", 10.0f),
    d2 = new Font("Arial", 10.0f), 
    d3 = new MemoryStream())
{
    var stream1 = (MemoryStream)d3;
    stream1.WriteByte(0x30);
}

However, I'm not recommending this and I consider it abusive, so this answer is just to state that you can hack around it but you probably should not.

João Angelo
Vote up for showing us what *not* to do.
Jay Bazuzi