A Keyword I’d Like To See in C#

Although I use C# more and more these days, I still consider myself primarily a C++ developer. C# has a few really neat language features that are just now being added to C++ (like lambdas), but I still find C++ to be more expressive, with terser syntax, than C#.

One example of this is deterministic disposal in C#. For classes that implement IDispose this is accomplished with the using statement. One example from MSDN follows:

Seems simple enough, right? What could I possibly have to complain about in that code?

This is an example of a programming practice known by the clumsy acronym RAII, which stands for Resource Acquisition Is Initialization. No matter what happens inside the scope defined by the braces (short of abnormal termination), the font1 object will be properly disposed when execution leaves the scope.

In C++ this idiom is very common. For example, if you need to define a temporary buffer but you won’t know the size of the buffer until runtime, you can use a std::vector<T>. You don’t have to worry about allocating or freeing memory because the vector class takes care of it for you. Here’s an utterly contrived example:

When execution reaches the end of scope, the vector destructor takes care of deleting the memory it allocated in the constructor. What’s nice about the C++ way of doing things is that there’s no using statement or other special syntax decorations. This is just how C++ works.

Now, C# doesn’t have true destructors like C++, so this exact syntax can’t work in C#. It only has finalizers, and there’s no way to predict whether, or even if, finalizers will be executed. This is why .NET provides the IDispose pattern. The thing is, I find the syntax for deterministic disposal to be clumsy. I think it would be a little easier to use if there were a keyword that I could use to decorate objects that I want to deterministically dispose. We could name this mythical keyword raii. Applied to the first example, it would look like this:

The raii keyword would instruct the compiler to emit the same code that it would emit for the using statement, so that the object would be disposed at the end of scope, but the syntax is a little cleaner. You could even stack more than one disposable object in the same block with little effort:

Both font1 and foo would be disposed, in reverse order, at end of scope.

I can’t find a reason not to do this. Can anyone else think of some downside that I’ve missed?

UPDATE: Roger Pate points out below that a new keyword isn’t necessary; just make the using statement affect the rest of the current block rather than requiring a new block below it, and you’re there.

While I wish they’d picked a better keyword than “using” to begin with, this change would still make it a little less clumsy.

I’ve also learned, since writing this article, that it’s possible to stack the using statements so that they enclose a single block. This should be obvious, but it never occurred to me.



  1. You don’t even need a new keyword; “using” could be extended so when it appears as a statement without an attached block, it automatically “wraps” the remainder of the block containing it:

    “using var font = new Font(); using var another = …; foo(); bar();” => “using (var font = new Font()) { using (var another = …) { foo(); bar(); } }”

Leave a Reply

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