I know this is a bit off topic, but I just had to comment about it here.

Since I was not able to attend to PDC I’ve been hearing a lot about the announcements and new stuff coming out of it.

One of these things is some of the features in C# 3.0 spec called “Extension Methods”.
In short, Extension Methods allows you to extend an existing class that is sealed by creating a class that has a static function with a special syntax which allows you to call that function on an instance of that object as if it was a member function.

For example, if I want to extend the the String class (which, as you all know, is a sealed class and has its reasons to be like that which I won’t get into at the moment) I would create a class like this:

public static class MyExtension {
public static void Foo(this string s) {
Console.WriteLine(“MyExtension::Foo = {0}”, s);
}
}

Notice the “this” keyword near the function’s parameter. This is the keyword that does the magic.

To invoke this method I would use the following syntax:

string s = “Testing Extensions”;
s.Foo();

You see, it looks like the String class always had a method named Foo.

Now as much as some people think this feature is cool I don’t see the point of actually having it and showing it like a big thing.

This “functionality” could have been used in previous version of C#. In previous version all you had to do is this:

public static class MyExtension {
public static void Foo(string s) {
Console.WriteLine(“MyExtension::Foo = {0}”, s);
}
}

and invoke the code like this:

string s = “Testing Extensions”;
MyExtension.Foo(s);

Which, in my opinion, is a lot clearer when reading this code since I know that there is a class somewhere in the code (either from a reference or inside the same .cs file or in another .cs file in the project) that is called MyExtension and have a method called Foo.

So why do we need this functionality?

I guess we don’t, but the only reason I can think of came to me from seeing all the examples out there which involves the String class. Perhaps because the String class (and a few other classes in the Base Class Library (BCL)) are sealed, some people whined too much about it and someone decided on adding this to make them get the same feel as if they inherited from a sealed class and extended it.

Now I know that the C# Spec is saying: “Extension methods are less discoverable and more limited in functionality than instance methods. For those reasons, it is recommended that extension methods be used sparingly and only in situations where instance methods are not feasible or possible.”

But what I do know is that every feature which is not very specific and limited and can be used in a wrongful way, WILL be used in a wrongful way.

So the main question here is, why add such a feature if there are a LOT of other features that are more needed and can get a enchance productivity?

(I wonder how can one get into the language design meetings or at least for the review process of the specs before they are submitted 😉 ).