Welcome to irritatedVowel.com Sign in | Help

POKE 53280,0: Pete Brown's Blog

Silverlight, WPF, Woodworking, .NET Programming, CNC, Nature, and other topics.
Pete is an MVP for Silverlight Click here to view the MVP profile.

A Microsoft Silverlight MVP and INETA Speaker, Pete Brown writes on a number of topics including Silverlight, WPF, .NET, woodworking and working as a consultant in the DC area. read more

Subscribe

Subscribe to my feed
Add to Technorati Favorites

Community Events



Applied Information Sciences - My Employer

who's online

AddThis Social Bookmark Button

Extension Methods in Silverlight and .NET 3.5

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.

  Add to Technorati Favorites
Posted: Tuesday, October 30, 2007 2:10 PM by Pete.Brown
Filed under: ,

Comments

Christopher Steen said:

Link Blogs 21 Links Today (2007-10-29) [Via: Matt ] Interesting Finds: October 30, 2007 [Via: Jason...
# October 31, 2007 6:47 AM

Christopher Steen said:

Link Listing - October 30, 2007
# October 31, 2007 6:47 AM

POKE 53280,0: Pete Brown's Blog said:

Over the past couple nights, I've had a few hours to work on a wallpaper viewer for my site. As part
# November 1, 2007 1:10 AM

POKE 53280,0: Pete Brown's Blog said:

I spoke about extension methods in C# 3.0 and VB9 tonight at CMAP . Here are the links I promised: This
# November 6, 2007 11:08 PM

Pete.Brown said:

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.

# November 6, 2007 11:09 PM

Jesse Liberty - Silverlight Geek said:

  A number of articles I've read recently have mentioned "Extension methods" in passing
# February 7, 2009 1:08 PM

Jesse Liberty - Silverlight Geek said:

A number of articles I've read recently have mentioned "Extension methods" in passing, yet it is difficult
# February 7, 2009 1:18 PM

Microsoft Weblogs said:

  A number of articles I've read recently have mentioned "Extension methods" in passing
# February 7, 2009 1:49 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Enter the text you see in the image:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS