Orcas Extension Methods
I've been having a look at extension methods lately (see more info on them here and here) and at first pass they seem really quite nice. They offer a easy way to augment existing object models which you may not own (from a code perspective). Consider this example:
public static class E
{
public static String TestMe(this string s, string msg)
{
Console.WriteLine(s);
return s;
}
}
I can then use this method directly on the String class itself like this:
String s = "Hi";
String s1 = s.TestMe("Hi^2");
So far so good, the method to operate on String's now directly appears on the String class itself making for a more natural and usable design.
It's at this point that you're wondering what problem could there be with this. Well, it turns out that the rub (and this was revealed this week and after debating the merits of this feature with a collegue this week) is that if you have an Extension method, say the TestMe method shown above, and the String class introduces a method called, "TestMe" you will recieve no error (or even a warning!).
Now, this seems like an odd design to choice to me. It seems like I should have, at a minimum, be given the opporunity to recieve a warning and to elevate this to an error if I know that I have extension methods in my solution. Does anyone else agree this is an odd design choice?
Comments
- Anonymous
November 12, 2007
PingBack from http://msdnrss.thecoderblogs.com/2007/11/13/orcas-extension-methods/