Why doesn't 'using' command allow multiple objects?

I

Israel

I always wondered why the using command can only take one object. I
always find that this isn't sufficient for drawing so I end up always
disposing in my finally block but then I have to remember to put
everything in there.
Why can't the using command just allow something like this?

using (Pen mypen = new Pen(Color.Black), Brush mybrush = new
SolidBrush(Color.Blue))
{
}

I thought about creating some object that takes an array of
IDisposable objects in its constructor and then calls their dispose
methods from its dispose method but that would be a very clunky
interface to deal with. I would much rather just use a try...finally
pattern.
 
J

Jon Skeet [C# MVP]

Israel said:
I always wondered why the using command can only take one object.

It can create multiple objects of the same type, but not of different
types.
I always find that this isn't sufficient for drawing so I end up always
disposing in my finally block but then I have to remember to put
everything in there.

Why not just nest using statements?

using (Pen mypen = new Pen(Color.Black))
{
using (Brush mybrush = new SolidBrush(Color.Blue))
{
}
}

Alternatively, you don't need the outer braces - using statements stack
nicely:

using (Pen mypen = new Pen(Color.Black))
using (Brush mybrush = new SolidBrush(Color.Blue))
{
}

You should be aware, by the way, that the using statement isn't like a
normal method - it's a language construct.
 
I

Israel

Why not just nest using statements?
I guess it just bothered me to have multiple indentations. Mostly for
preference reasons but also because if had two disposable objects used
within an area and then later changed that to 3 and only added a
single line within that region then the file would look like I made a
whole bunch of changes to many lines (unless I ignored whitespace
changes) because it had to indent a bunch of code. At what point does
the indentation become overwelming? 5, 6 or 7 levels?
Alternatively, you don't need the outer braces - using statements stack
nicely:
I wasn't aware of that but it makes sense since that's how the 'if'
statement works. I guess that's another personal thing but I always
make it a policy to never use 'if' statements without braces because
it scares me; the conditional code isn't properly contained with its
own scope and could spill out all over the place :) Also it was a
coding standard at my first job out of college and I guess it just
stuck with me.
You should be aware, by the way, that the using statement isn't like a
normal method - it's a language construct.

I guess that's why I figured they could have easily done whatever they
wanted when they first created C# since C++ doesn't have anything like
that. Was 'using' barrowed from VB6? Or was that a different using
because they didn't have the concept of dispose before .NET.
 
J

Jon Skeet [C# MVP]

Israel said:
I guess it just bothered me to have multiple indentations. Mostly for
preference reasons but also because if had two disposable objects used
within an area and then later changed that to 3 and only added a
single line within that region then the file would look like I made a
whole bunch of changes to many lines (unless I ignored whitespace
changes) because it had to indent a bunch of code. At what point does
the indentation become overwelming? 5, 6 or 7 levels?

If you've got more than 4 IDisposables in scope at a time, it's worth
refactoring IMO. However, with pretty wide monitors these days line
limits aren't what they were. I try to avoid having too much
*non-whitespace* per line, but having a reasonably large indentation
doesn't bother me much.

I've also found that changing indentation to 2 spaces instead of 4
makes code a lot nicer to read.
I wasn't aware of that but it makes sense since that's how the 'if'
statement works. I guess that's another personal thing but I always
make it a policy to never use 'if' statements without braces because
it scares me; the conditional code isn't properly contained with its
own scope and could spill out all over the place :) Also it was a
coding standard at my first job out of college and I guess it just
stuck with me.

I do the same - and in fact I almost always indent using statements
too, but then I don't mind the indentation.
I guess that's why I figured they could have easily done whatever they
wanted when they first created C# since C++ doesn't have anything like
that. Was 'using' barrowed from VB6? Or was that a different using
because they didn't have the concept of dispose before .NET.

As far as I'm aware the "using" statement originated with C#. And yes,
they potentially could have made it declare more than one variable, but
I don't see it as a significant flaw that they didn't.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top