Things I didn’t know (or remember) about the C# using statement

Declaring disposables outside the using statement

The first thing that surprised me was that you don’t have to declare the IDisposable in the using statement. Maybe because something like this:

public class Foo : IDisposable 
	Bar bar = new Bar();
	public void Dispose() 

doesn’t quite feel natural to me. However, MSDN does mention that this usage is possible, so I shouldn’t be surprised. I came across this one when trying to avoid writing a try/finally pair to dispose of a member variable inside a Dispose method.

Multiple IDisposables in a single using statement

While browsing the documentation for the using statement, I came across this gem:

Multiple objects can be used in with a using statement, but they must be declared inside the using statement, like this:

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

I’m beginning to wonder where it was I learned the using statement from, given that I really didn’t know this is supported. Granted, I learned my C# basics when the language was version 1.0, and while the older reference does have an example of this usage, it’s not as clear as the current version. Still, lacking this knowledge meant that I’ve (ab)used braceless blocks to achieve the same effect:

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

which not only is questionable in terms of syntax abuse, but also more verbose than the better way.

Variables declared inside a using statement are read-only

This one is courtesy of ReSharper’s background compilation. I was cleaning up code that opened a database connection twice by moving the opening statement to the outer scope and converting a try/finally pair to a using statement, when I got this message (I’m futzing with File.Open here purely for illustrative purposes):

ReSharper showing an error when trying to assign to to a variable declared inside a using statement

This makes a lot of sense, but is still somewhat surprising, given that C# does not have a way to declare read-only local variables. Another thing that seems a bit inconsistent to me is that when the declaration is outside the using statement, the compiler doesn’t mind if you assign to it:

When the declaration is moved outside the using statement, reassigning is possible again

which doesn’t make so much sense to me. I’m sure there’s a reason, but when I read that, I’m thinking whatever is assigned to foo before the using statement will be disposed of. I’m fairly sure that isn’t the case when reassigning is allowed.

So if I didn’t know this about the using statement, how well do I know C#?

I thought I knew the C# language pretty well, since it has been my primary tool at work for quite some time now. Turns out I was really, really wrong. I think I’m going to get a C# 3.0 language reference book just to go through the different aspects of the language and see if there are other places where I’ve missed something obvious.

The bigger question is, how can I ever hope to master even this one language, if details like this have eluded me for over four years, especially since C# is evolving rapidly.

Leave a Reply

Your email address will not be published. Required fields are marked *