Signum Framework Logo
"Open framework that encourages convention over configuration, using C# code,
not XML files, to model at the right level of abstraction and achieve deadlines.
...but also has a full Linq provider, and syncs the schema for you!"
Login
RSS

Search

»



Main
Index
Map
Videos
Download
Source code
Tutorials
Forum
FAQ



Image



PoweredBy

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.

Creative Commons License Signum Framework Site by Signum Software is licensed under a Creative Commons Attribution 3.0 License.
Powered by ScrewTurn Wiki version 3.0.5.600.