What is an extension method?
ExtensionMethods is a nice feature added in C# 3.0 that allow us to 'pretend' that a method is an instance method of an object where really it's defined in a static class outside of the type. This small difference can make a huge step forward in readability:
Code with Linq query like this:
var query2 = people
.Where(p => p.Age > 20)
.OrderByDescending(p => p.Age)
.ThenBy(p => p.Name));
will look like this without extension methods:
var query2 = Enumerable.ThenBy(
Enumerable.OrderBy(
Enumerable.Where(
people,
p => p.Age > 20),
p => p.Age),
p => p.Name):
because all these methods are not implemented over IEnumerable<T>.
The library avalanche (Advanced Topic)
This language feature has become so useful that many utility projects have grown around the concept, to mention just a few:
There's also an active
StackOverflow question about this with
his own CodePlex project, and a
database of Extension Methods!
It looks like everybody wants to make an 'standard' set of extension methods, and in the way they introduce a new library, making the problem bigger.
So, why bother doing a new one?
- Consistency: It looks better for Signum Framework to depend on Signum.Utilities than on an external library.
- Control: We prefer to have control over the library so we can add code we need.
- Clutter: Extension methods tend to create clutter on your IntelliSense. We follow LINQ Framework Design Guidelines.