Skip to main content

Abusing using

When we went from unmanaged to managed languages we got rid of the headache of having to do manual memory management. Unfortunately that meant we also lost destructors and with them the ability to do some nice tricks. Luckily we can still do some of these tricks with the using keyword. Lets take a look

One of the nice things you could do with destructors in c++ was to tie the execution of a piece of code to the scope of a variable. You did this by putting the piece of code in a destructor of some object, every time you instantiated that object on the stack you were assured your code would be run when that object left scope.

But wait, we still can do this with using and IDisposable.

Oh boy! Silly example time!

Lets say you’ve got a bunch of freeloading friends. You kept score on how many rounds of beer you bought last night in a dictionary and now you want to confront them with the ugly truth by putting the contents of that dictionary online in an html table.

var rounds = new Dictionary<string,int> {{"Me", 6}, {"Bill", 3}, {"Pete", 1}};

Formatting html tables can be a lot of busywork. You need to keep track of all elements. Every <td> needs a </td>. This can get messy fast!

First we create a helper class that will print and keep track of elements:

   1: class HtmlElement : IDisposable
   2: {
   3:     private string Element;
   5:     public HtmlElement(string element)
   6:     {
   7:         Element = element;
   8:         Console.WriteLine("<{0}>", Element);
   9:     }
  11:     public void Dispose()
  12:     {
  13:         Console.WriteLine("</{0}>", Element);
  14:     }
  15: }

Now printing tables (or any kind of simple html) is easy.

   1: using(new HtmlElement("table"))
   2:     foreach (var row in rounds)
   3:         using (new HtmlElement("tr"))
   4:         {
   5:             using (new HtmlElement("td")) Console.WriteLine(row.Key);
   6:             using (new HtmlElement("td")) Console.WriteLine(row.Value);
   7:         }

Every html element we print is tied to the scope of the using block it’s in. When we get to the end of the block the closing element is automatically added. Simple and pretty readable.

I’ve seen this trick used in all kinds of places where you want a piece of code guaranteed to run after some other operation. Transactions in nHibernate use the same mechanism for example. Keeps you from typing catch/finally blocks around every transaction.


Popular posts from this blog

Using xUnit.Net with .Net 4.0

I’ve been using xUnit.Net for a while now. It’s just a tiny bit cleaner and slightly less abrasive than other .Net unit testing frameworks. Leaving out unnecessary stuff like [TestFixture] and shortening Assert.AreEqual to the equally clear but shorter Assert.Equal don’t seem like big improvements but when you type them several times a day tiny improvements start to add up. I also like the use of the [Fact] attribute instead of [Test]. It shifts the focus from testing to defining behavior. So how do we get all this goodness working with the Visual Studio 2010 beta?

Running a Git repository on Ubuntu using Gitosis

20I’ve been using Git for a couple of small projects that I’ve been hosting on but version control for my bigger ‘secret’ projects still runs on a windows machine with visual svn server.Now that I’m starting to use Mono for a couple of projects so I’m playing with linux more. Last week I decided to try to try out gitosis on an ubuntu server. I found out it’s pretty easy to use when you know your way around git but for a noob like me some things weren’t immediately clear. Eventually I solved most problems I ran into, so I decided to write up the steps I took to install gitosis on ubuntu 10.04

Android development resource links

I've been playing with the Android SDK and I have a growing list of bookmarks to Android dev resources for my own use. I thought the best place to keep them would be here on my blog. That way other people can benefit too. I'll keep updating this list so feel free to add suggestions in the comments.