A Critical Look at C# 3.0 Extension Methods

Ian Griffiths takes an in-depth look at C# 3.0 Extension methods and the potential problems with it. Of particular note is his philosophy, which directly follows from the idea that code should be written for humans, which he summarizes whe he say...

I’m a big fan of code that does what it looks like it does.

Amen brother!

As an example, he highlights the ToUpper method on a System.String instance, which often misleads new developers. He would prefer the more honest and less misleading static method on the String class that would be called like so:

String.ToUpper(input);

I agree wholeheartedly that ToUpper (which sort of follows the Java convention I guess) is misnamed, but (and this really is a minor niggle) I probably would prefer that it still be an instance method, but renamed GetUpperCase. I think that would do a good enough job of being honest and being discoverable.

In any case, if you’re interested in C# 3.0, be sure to read Ian’s take on extension methods.

What others have said

Requesting Gravatar... Ian Griffiths Oct 03, 2005 1:45 AM
# re: A Critical Look at C# 3.0 Extension Methods
Actually, yes, on reflection I prefer your solution of a better name for ToUpper, rather than my more violent change.

Of course it's too late for either solution... But at least we can relax, knowing that a solution exists.
Requesting Gravatar... Haacked Oct 03, 2005 4:11 AM
# re: A Critical Look at C# 3.0 Extension Methods
Well since you're planning to write a compiler, you could perhaps also write a new language that corrects all the mistakes in C#. Call it C#++. ;)

What do you have to say?

(will show your gravatar)
Please add 3 and 3 and type the answer here: