views:

490

answers:

9
+1  A: 

From my answer here:

As regards a practical use for Extension Methods, you might add new methods to a class without deriving a new class.

Take a look at the following example:

public class extended {
    public int sum() {
        return 7+3+2;
    }
}

public static class extending {
    public static float average(this extended extnd) {
        return extnd.sum() / 3;
    }
}

As you see, the class Extending is adding a method named average to class Extended. To get the average, you call average method, as it belongs to extended class:

extended ex = new extended();

Console.WriteLine(ex.average());

Reference: http://aspguy.wordpress.com/2008/07/03/a-practical-use-of-serialization-and-extension-methods-in-c-30/


As regards Performance, I think you can see an improvement with Extension methods since they are never dispatched dynamically, but it all depends on how the dynamic method is implemented.

Andreas Grech
+1  A: 

Its beeing used to extend (add on) functionality of existing classes without actually changing them.

You can see this in how LINQ (System.Linq namespace and others) adds on a lot of functionality to all collections.

Arjan Einbu
A: 

I'm not aware of any performance implications. Using extension methods makes most sense when you don't have access to the source code and thus can't add the method to the class directly, and the method makes sense to be implemented as a function. This goes to the comment I made on your previous question, where one other person gave a sample for an extension method on the 'string' class that returned a bool based on whether the string was a valid email. This, IMO, is an example of when NOT to use an extension method, because that function isn't fundamental to the string type. Adding a Left(int) and a Right(int) function to 'string' does make sense, however.

Michael Bray
+7  A: 

When does using extension methods make sense?

They make sense when you are using LINQ and want to chain or pipe functional output from one function to another. It improves the readability of the code and allows you to express a concept more elegantly (whatever that is worth).

They also allow you to give the appearance of instance methods on any type you like without modifying the source of that type, this can often help readability and the expressiveness of your code when it is used reasonably

Does adding extension methods to a type affect performance?

Note that an extension method call like:

instance.SomeExtensionMethod()

gets compiled to:

StaticExtensionMethodClass.SomeExtensionMethod(instance);

so performance will be the same as any other static method call.

Andrew Hare
Extension methods aren't restricted to only using them with LINQ.
Scott Dorman
A: 

Another interesting use of extension methods is when you would like to have certain functionality added to a class under one namespace but not another. One specific example is adding methods to ease unit testing - you wouldn't want them cluttering your production assemblies, but they're great to have when writing unit tests.

Isaac Overacker
+1  A: 

In addition to the other answers, extension methods are a great way of adding boiler-plate implementation to interfaces. For example, if you want all lists tto be sortable, add an extension method for IList<T>.

You can (as already stated) also use extension methods to add methods to classes outside of your control; ever wanted a Reverse() method on string? Add one!

The only difference is that extension methods don't use virtual, and there is no null-check. You can use this to your advantage if you like:

public static void ThrowIfNull<T>(this T obj, string name) where T : class
{
    if(obj == null) throw new ArgumentNullException(name);
}

Unlike regular utility methods, they make it very easy to write fluent interfaces; this is one of the reasons for their existance - i.e. with LINQ:

var foo = source.Where(predicate).OrderBy(selector);

is a lot more readable than:

var foo = Enumerable.OrderBy(Enumerable.Where(source,predicate),selector);

With regular methods, to use the first approach it would have to be regular instance methods, which would require changes (for example) to IEnumerable<T> - not desirable.

Marc Gravell
A: 

Extension methods shine wherever functional programming is used.

Consider almost any Module-level functions you already have in your app and what they become when you tag them as extension methods. They become an opportunity for "active voice" over "passive voice". Active voice means the code reads as though an instance provides its own method for performing a particular task, rather than having the function perform an action passively on it.

ExtendUnlimitedCredit(AddVIP(tblCustomer, "Bill Gates"))

vs.

tblCustomer.AddVIP("Bill Gates").ExtendUnlimitedCredit()

Extension methods make this transformation simple. Eliminating nested function calls with "active voice" code is often an improvement. Plus, since our shop locks down its core classes, we were only able to utilize nested function calls prior to extension methods.

The other great things about extension methods:

  1. They make your functions (because of Intellisense) more discoverable. And if you provided inline markup describing the purpose and use of your function, Intellisense will even provide a helpful tooltip describing the method and its use to the developer who discovers it (simply by pressing the dot). Functions not tagged as extension methods are not so readily discovered, may go unused and, as a result, someone else may invent their own flavor of said function.

  2. Whereas you cannot actually implement a method for an interface, a extension method offers an alternate means that gives the appearance that you’ve done so.

Mario
A: 

I use them to reuse my object model classes. I have a bunch of classes that represent objects that I have in a database. These classes are used in the client side only to display the objects so the basic usage is accessing properties.

public class Stock {
   public Code { get; private set; }
   public Name { get; private set; }
}

Because of that usage pattern I don't want to have business logic methods in these classes, so I make every business logic to be an extension method.

public static class StockExtender {
    public static List <Quote> GetQuotesByDate(this Stock s, DateTime date)
    {...}
}

This way I can use the same classes for business logic processing and for user interface displaying without overloading the client side with unnecessary code.

One interesting thing about this solution it's that my object model classes are dynamic generated using Mono.Cecil, so it would be very difficult to add business logic methods even if I wanted. I have a compiler that reads XML definition files and generate these stubs classes representing some object I have in the database. The only approach in this case is to extend them.

Augusto Radtke
A: 

Addressing your second question: my rule of thumb is that the extension method should be a "natural extension of functionality" on the type or that it should be a maintainable and readable part of a fluent interface.

An example of a "natural extension of functionality" is extending a data reader to return a default value if a DBNull is enountered. Not so natural would be extending data reader to return an instance of the entity represented by the data in multiple fields. In the latter case, you're injecting the wrong responsibilities into the poor object :).

Kit