O'Reilly logo

Visual Studio 2010 Best Practices by Peter Ritchie

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Working with extension methods

Along with lambda expressions and iterators, C# 3.0 brought us extension methods. These static methods (contained in a static class whose first argument is modified with the this modifier) were created for LINQ so IEnumerable types could be queried without needing to add copious amounts of methods to the IEnumerable interface.

An extension method has the basic form of:

public static class EnumerableExtensions
{
public static IEnumerable<int> IntegerSquares(
this IEnumerable<int> source)
{
return source.Select(value => value * value);
}
}

As stated earlier, extension methods must be within a static class, be a static method, and the first parameter must be modified with the this modifier.

Extension methods extend the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required