views:

126

answers:

7

Extension methods are really interesting for what they do, but I don't feel 100% confortable with the idea of creating a "class member" outside the class.

I prefer to avoid this practice as much as I can, but sometimes it looks better to use extension methods.

Which situations you think are good practices of usage for this feature?

+2  A: 

I think that best place for extension methods is "helper" methods or "shortcuts" that make existing API easier and cleanier by providing default values to arguments of existing methods or hiding repeating chains of method calls.

Contrary to the common beliefs that you can "extend" classes for which you do not have access to the source code, you cannot. You have no access to private methods and objects, all you can do is to polish public API and bend it to your likings (not recommended).

alex
+1  A: 

When the class is not extensible and you don't have control over the source code. Or if it is extensible, but you prefere to be able to use the existing type instead of your own type. I would only do the latter if the extension doesn't change the character of the class, but merely supplies (IMO) missing functionality.

tvanfosson
A: 

Extension methods are a great way to add functionality to classes that you don't own (no source), are in the framework or that you don't want to inherit for whatever reason.

I like them, but you are right. They should be used judiciously.

Paul Lefebvre
+2  A: 

They're great for interfaces (where you can add "composite" behaviour which only uses existing methods on the interface) - LINQ to Objects is the prime example of this.

They're also useful for creating fluent interfaces without impacting on the types that are being used. My favourite example is probably inappropriate for production code, but handy for unit tests:

DateTime birthday = 19.June(1976) + 8.Hours();

Basically anywhere that you don't want to or can't add behaviour to the type itself, but you want to make it easier to use the type, extension methods are worth considering. If you find yourself writing a bunch of static methods to do with a particular type, think about whether extension methods wouldn't make the calls to those methods look nicer.

Jon Skeet
+1  A: 

In my opinion, extension methods are useful to enhance the readability and thus maintainability of code. They seem to be be best on entities where either you have no access to the original class, or where the method breaks "Single Responsibility Principle" of the original class. An example of the latter we have here is DSLs. The DSL models are extended with extension methods are used to make T4 templating easier but no methods are added the model unless they are specifically related to the model.

Preet Sangha
+1  A: 

The ideal use for them is when you have an interface that will be implemented in many places, so you don't want to put a huge burden on implementors, but you want the interface to be convenient to use from the caller's perspective as well.

So you put the "helpers" into a set of extension methods, leaving the interface itself nice and lean.

interface IZoomable
{
    double ZoomLevel { get; set; }
}

public static void SetDefaultZoom(this IZoomable z)
{
    z.ZoomLevel = 100;
}
Daniel Earwicker