I’ve played classical piano since I was seven (long before most readers were born, that’s for sure!) but I stopped taking lessons about the time I was 18 or so. I’ve played, off and on, ever since, but if I was ever good, I’ve long since lost most of the skill. After many years of working fourteen-hour days, and finding little time to play the Steinway in the living room, one of my goals for “mid-life” was to play more, and I’ve been relatively successful in meeting that goal now that I’ve settled in rural America.

I’ve always loved playing with other people-I’ve never been quite good enough to play in public alone, but with another good player, my mistakes can be hidden! Back in the frantic 80’s, when I was in my thirties, I ran across a piece written for two pianists on two pianos, by the Russian composer Rachmaninoff (who, apparently, had huge hands, and he wrote music that he could play, but most others struggled with). The Suite No. 2, Opus 17, is an amazingly muscular, touching, and glorious piece for two experienced pianists. It’s incredibly dense, and very rich. Even if you can’t read music, check out the tiny excerpt in Figure 1-so many notes!

Figure 1: Rachmaninoff writes a dense piece of music.

Since first hearing this piece, it’s been a dream of mine to bang through it with another pianist. The problem, of course, is finding two decent pianos in the same place, and finding someone stupid enough to spend time trying to get through this piece with me, considering that it’s a piece that’s far beyond my reach.

It turns out that my neighbor took piano lessons for a while from a semi-retired concert pianist who lives locally. Figuring that we’d get along, the neighbor has invited both of us to dinner on several occasions, and each time, the pianist (Aileen) has graciously played some simple duets with me-this is the equivalent of an ardent basketball fan getting to play one on one with (insert your favorite basketball star here). Each time we did this, I’d end up somewhat embarrassed and highly exhilarated, given the opportunity to play with a master. Of course, the results sounded great, because Aileen’s playing “cancelled out” my banging.

Recently, we repeated this annual event. This time was different however-because I’d recently purchased an electronic piano, I realized that I could schlep the piano next door and plop it down next to the grand piano: Finally, I’d be able to play my beloved Suite No. 2. As it happens, Aileen had performed the piece in concerts twice over the past few years, and was up for the challenge. The neighbor had invited not just us, but six other friends to enjoy the “concert”, and after dinner, we sat down to play. I explained that I had practiced alone at a very slow speed (and Aileen and I never had practiced together, which really is a requirement for this sort of playing), but we gamely gave it a try.

I counted off the speed I was willing to try, and we headed out of the gate. About 10 seconds in, Aileen was playing at full concert speed, and I was doing my best to keep up. Imagine being tied behind a car, trying to run along with the moving car. At 2 MPH, you’re doing fine. Suddenly, the car speeds up to 10 MPH. No longer so fine, are you? I was sweating, cursing under my breath, and just doing everything I could to keep up. Amazingly, we ended at the same time for each of the piece’s four movements. By the end, I was so winded I could barely breath. But boy, was it fun. All I can suggest is that whatever you do, as an amateur, try to find a pro to do it with-the workout is worth it!

And speaking of extending beyond your reach (yes, a very lame transition), the versions of Visual Basic and C# in Visual Studio 2008 add a new feature that you may have missed-the ability to extend an existing class; that is, the ability to add extension methods to a class without the need to inherit from the original class (which creates a new class, with a new name).

As a simple example, imagine that you need to often determine if an integer is odd, as part of an application. Sure, you could easily write an IsOdd method, but that would require you to pass the integer in question to the procedure. Wouldn’t it be better if you could just write code like this (assuming that value is an Int32 variable)?

' Visual Basic:
If value.IsOdd Then
  ' Handle odd values...
End If
// C#:
if (value.IsOdd())
{
  // Handle odd value...
}

In other words, it would be great if you could just extend the existing functionality of a class, even if you didn’t have the source code for the class available. You can, of course, using the new extension method functionality built into C# 3.0 and Visual Basic 9.0 (the languages in Visual Studio 2008). In order for this to work, you must follow some simple rules. In order for a procedure to be treated as an extension method, it must:

  • Accept a parameter, the type of which defines the class that you’re extending.
  • VB: Exist within a module.
  • VB: Include the <Extension()> attribute. (Import the System.Runtime.CompilerServices namespace to add this attribute.)
  • C#: Exist within a static class.
  • C#: Be a static method.
  • C#: Include the this keyword in the parameter declaration.

To test this out, you can create a new class (in C#) or module (in VB), and name it Extensions (the name is arbitrary-choose any name you like). In C#, add the static keyword to the class:

// C#:
static class Extensions
{
}

In VB, add the Imports statement to the top of the file:

' Visual Basic:
Imports System.Runtime.CompilerServices

Within the class, add the following code:

' Visual Basic:
<Extension()> _
Public Function IsOdd( _
 ByVal value As Integer) As Boolean
    
  Return Not value.IsEven
End Function
    
<Extension()> _
Public Function IsEven( _
 ByVal value As Integer) As Boolean
    
  Return value Mod 2 = 0
End Function
    
    
// C#:
public static bool IsOdd(this int value)
{
  return !value.IsEven();
}
    
public static bool IsEven(this int value)
{
  return value % 2 == 0;
}

Note that in each case, the parameter type indicates the class that the extension method extends. In Visual Basic, note the <Extension> attribute, and in C#, note the this keyword, which indicates that the method is an extension method.

Compile your project and add code like the code shown previously to test the extension method(s). As you see, you’ve added two new methods to the Int32 class, allowing you to easily extend the functionality of the built-in class. This technique provides a great way to refactor your code, making it easier to use, and easier to maintain.

It’s easy to extend the reach of .NET Framework classes using the new Extension method functionality. As for extending your reach in other areas, get yourself under the thumb of someone who’s a lot better than you are-it may cause some sweating and cursing as you try to keep up, but it sure can be a lot of fun! (In my case, Aileen has suggested, for the first time, that we get together for private time to play duets; in other words, I’m back to piano lessons. It will be fun, amidst the suffering, I’m sure!)