Welcome to Pete Brown's 10rem.net

First time here? If you are a developer or are interested in Microsoft tools and technology, please consider subscribing to the latest posts.

You may also be interested in my blog archives, the articles section, or some of my lab projects such as the C64 emulator written in Silverlight.

(hide this)

Extension Methods in Silverlight and .NET 3.5

Pete Brown - 30 October 2007

In my spare time, I've been working on a Silverlight sample that can be used to browse the wallpaper on my main web site.

As part of that, I have been implementing a custom panel with layout/positioning code. Since all coordinates in Silverlight are double values, you can sometimes get stuck in situations where you need to treat 1.0000001 and 1.00001 as the same value. Rather than implement a helper class or a base class (both of which are things I typically do), I decided to try an extension method and make it available any place there is a double value in my code.

(Silverlight hosts a version of the .NET 3.5 framework, so this information will generally apply to both windows CLR and agclr applications. )

Creating an extension method is pretty simple. Just define a static class with a static method.

public static class Extensions
{
    // extends the double class to tell us if something is within
    // a reasonable tolerance of the indicated value.
    public static bool WithinToleranceOf(this double d1, double d2)
    {
        return WithinToleranceOf(d1, d2, 0.001);
    }

    public static bool WithinToleranceOf(this double d1, double d2, double tolerance)
    {
        return Math.Abs(d1 - d2) < tolerance;
    }
}

Now any place in my code where I have a double value, and am using the namespace within which the Extensions class exists, the double is extended with the WithinToleranceOf method. Intellisense for the double type shows:

It reminds me of some of the earlier C and C++ code, or at least the C code some early C++ compilers generated in that the first parameter to the function is a reference to the type you are attaching to.

Operator overloading is also an approach to this problem, but as of the last time I checked, you could not overload operators via extension methods. Given that I have to get around limitations of inheritance here, I would have to go at it externally, so that would not work.

While legitimate arguments against the purity of such an approach in an OO language can be made, there's no doubt as to its usefulness in real-world applications.

   
posted by Pete Brown on Tuesday, October 30, 2007
filed under:    

1 comment for “Extension Methods in Silverlight and .NET 3.5”

  1. Pete Brownsays:
    Just a clarification, since I am guilty of this mistake from time-to-time, including here:

    Extension methods are a feature of VB9 and C# 3.0, not of .NET 3.5. It just so happens that all roll out as a package with Visual Studio 2008. You can use extension methods with .NET 2.0-targeted applications if you implement your own ExtensionAttribute class, and compile using the compilers from Visual Studio 2008.

Comment on this Post

Remember me

7 trackbacks for “Extension Methods in Silverlight and .NET 3.5”

  1. Christopher Steensays:
    Link Blogs 21 Links Today (2007-10-29) [Via: Matt ] Interesting Finds: October 30, 2007 [Via: Jason...
  2. POKE 53280,0: Pete Brown's Blogsays:
    Over the past couple nights, I've had a few hours to work on a wallpaper viewer for my site. As part
  3. POKE 53280,0: Pete Brown's Blogsays:
    I spoke about extension methods in C# 3.0 and VB9 tonight at CMAP . Here are the links I promised: This
  4. Jesse Liberty - Silverlight Geeksays:
      A number of articles I've read recently have mentioned "Extension methods" in passing
  5. Jesse Liberty - Silverlight Geeksays:
    A number of articles I've read recently have mentioned "Extension methods" in passing, yet it is difficult
  6. Microsoft Weblogssays:
      A number of articles I've read recently have mentioned "Extension methods" in passing