harriyott.com

Friday, June 20, 2008

Extension methods in C#

I recently needed to encrypt a string with MD5 encryption. Not having used this before, I looked at the MSDN page for the MD5 class. There was a ComputeHash method, but no overload took a string parameter - just a stream or a byte array.

I may be naive, but I would have thought encrypting a string would be a common enough task to warrant its own overload, but apparently not. It is common enough though, for some example code that converts the string to a byte array, calls ComputeHash, and converts the result to a hex string. This example seems to work fine, so I don't know why it isn't just squished into the class in the framework.

Not wanting to just add this example to the class I was working on, I thought I would write my first ever extension method, and add the example as an overload to the MD5 framework class. I read about extension methods in the marvelous Pro C# 2008 and the .NET 3.5 Plaform, which are straightforward, but the syntax is a little unintuitive:

public static class MD5Extensions
{
public static string ComputeHash(this MD5 md5, string input)
{
byte[] data = md5.ComputeHash(Encoding.Default.GetBytes(input));
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
return sBuilder.ToString();
}
}


In short, both the class and the method should be defined as static, and the first parameter has "this" before the type, which is the class that is being extended. I'm going to have to keep looking this up, as there are three things to remember. I would have preferred a new keyword, extended, which works in the same way as partial classes, like so:
public extended class MD5
{
public string ComputeHash(string input)
{
...
}
}


This would have been far easier to remember, and would actually look like the programmer's intention. I'm sure there must be a good reason for the way it has been done though.

Monday, June 02, 2008

Re-inventing the toll

One of the side-effects of being a developer is that I often find myself "debugging" things outside of the IDE, or the computer itself. Recently I went somewhere that involved driving across the Dartford crossing on the M25*. There's a tunnel under the River Thames for northbound traffic, and a huge bridge for southbound traffic. There are toll gates for both directions south of the river.

As I was in a long, southbound queue (caused by the toll) on my return journey, I started thinking about why there were two tolls in operation at the same time, and two resultant traffic jams, and what to do about it. I think I've come up with a way to ease the jams.

The obvious first choice is to dispense with the tolls altogether, since the bridge paid for itself in 2003. The government aren't going to do that though.

I reckon that almost everyone that uses the bridge in one direction uses it again in the opposite direction very shortly afterwards. In my case, it was about 7 hours after. Very few people make journeys from their home or place of work and don't return via the same route in the opposite direction.

On my journey, I had two toll transactions, of £1 each way. I think it would be better to have one transaction of £2 instead, that would cover both journeys. In this way, one set of tolls could be closed, and allow traffic to flow freely through, removing one traffic jam altogether.

Depending on the traffic flow, the tolls could be changed over to reduce an extremely long tailback. If this happened in the middle of the day, then some people may have to pay twice, and others nothing at all. Although this doesn't sound fair, it should even out over time.

Should I suggest this to Boris, or is it a silly idea?

* Actually, it isn't the M25 at the crossing, it's the A282. I presume this is so that learner drivers can cross the river without going into town, as they're not allowed on motorways.

NxtGenUG FEST08

Dave McMahon has asked me to let you know about FEST08. In his words:

"FEST08 the annual NxtGenUG one-day event takes place ar Microsoft Reading on Thursday 12th June. As ever it's going to be an action packed day with great content from the likes of Mike Taulty ,Oliver Sturm , Dave Sussman and other top speakers. No doubt there will be bundles of 'swag' and prizes and Pizza somewhere down the line - there always is when the nxtGenUG Boyz are around. There seems to be a few more of them this year with the Cambridge and Southampton crews joining in the mix.

So got to http://www.nxtgenug.net/fest08/ for details and to register your place. It's free to all NxtGenUG members and a mere £49.99 to non-members - bargain! Oh and also if you're around the night before there is a G(r)eek dinner to toast Daniel Moth on his way to the states. http://www.nxtgenug.net/ViewEvent.aspx?EventID=140 is the link to signup to."