5
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?

7 Answers 7

28
using(Font f1 = new Font("Arial",10.0f))
using (Font f2 = new Font("Arial", 10.0f))
using (Stream s = new MemoryStream())
{

}

Like so?

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

Comments

10

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
}

6 Comments

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.
This completely depends on an individual. You use it the way you like or prefer.
Though it should go without saying, consistency is the most important thing here.
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.
The order of disposal is same in both the cases. font3 will get disposed prior to font4 in both the cases.
|
6

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
}

Comments

5

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.

Comments

3

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

2 Comments

I've made a comment on your blog regarding the foreach example following your link.
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.
3

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())
    {

    }
}

1 Comment

Actually, you can have multiple objects of the same type, as the question shows.
2

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

3 Comments

Do you have a link to documentation which says they dispose in reverse order?
I think I heard it on a course, however, stacking them is the same as nesting them. See amended text.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.