Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extension methods versus inheritance

Are there rules of thumb that help determine which to use in what case? Should I prefer one over the other most times?

Thanks!

like image 648
djcouchycouch Avatar asked Jul 15 '09 15:07

djcouchycouch


People also ask

What is Extensionmethod why the extension methods can we achieve the same thing using inheritance?

The main advantage of the extension method is to add new methods in the existing class without using inheritance. You can add new methods in the existing class without modifying the source code of the existing class. It can also work with sealed class.

What is extension in inheritance?

extension -- subclass adds new methods, and perhaps redefines inherited ones as well. limitation -- the subclass restricts the inherited behavior. Violates substitutability. Example: defining Queue as a subclass of Dequeue. combination -- multiple inheritance.

What does extension method mean?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.


4 Answers

Extension methods are useful, but they are harder to discover through the IDE than regular methods, since they are not attached to the original class and there are no clues as to where the code for them might reside. There are some best practice suggestions as to where to put them and how to name them, but these are only guidelines and there is no guarantee that someone will follow them.

Usually you would use extension methods if you are only adding functionality to a well known, well used class or interface such as the .Net base classes, that you don't have access to the code for. Extension methods also have the constraint in that you not only have to have the original assembly, you have to have the assembly with the extension methods in it, which must be understood by consumers of your code.

Using inheritance will allow you to add, remove or override functionality, and ensure that it is always present with the class when you build it.

like image 54
womp Avatar answered Oct 23 '22 23:10

womp


Extension methods should be used when you want to provide an implementation across a variety of types that should share the same behavior, but would otherwise be disimilar. That's why you see extension methods being used on interfaces a lot, because it's a very powerful tool to ensure that any given implementation of an interface will have the same implementation of a given behavior.

For example, the Skip and Take extension methods.

like image 39
Joseph Avatar answered Oct 24 '22 00:10

Joseph


Well... you can't always use inheritance. String, for example, is a sealed class. It's in those cases where an extension method really shines.

In general, extension methods are best for little utilities that you might otherwise put into a static class, but that operate against an instance of a particular type. Strings are a great example -- almost everyone has their own little string extension methods to do little operations on a string.

Another great place for extension methods is against enumerations. I almost always include a HasFlag extension method against any [Flags] enumerations I create.

like image 10
Randolpho Avatar answered Oct 24 '22 01:10

Randolpho


Whenever possible, use inheritance instead of extension methods.

edit

I prefer to keep this short and simple, but I will of course answer follow-up questions.

In the cases where inheritance is possible, which is to say classes that are not sealed, it is almost always a better option than extension methods. In fact, this is what the best practices document that womp referenced says. It has headings such as "Be wary of extension methods", "Think twice before extending types you don't own", and "Prefer interface extensions over class extensions". In other words, it just says what my one-liner did, with greater detail.

The article does give detailed reasons, but the bottom line is that this is how extension methods were designed to be used. They were added to the language late in the game as a bit of syntactic sugar to allow MS to wedge in LINQ without having to go back and reinvent the wheel. This is the canonical example of what they are good for. Another good example is adding utility methods, such as:

public static string FormatWith(this string format, params object[] args)
{ return string.Format(CultureInfo.InvariantCulture, format, args); }

Note that, in this case, extension methods were the only way to accomplish this additional feature, since strings are sealed.

As for composition over inheritance, while this is a truism, I fail to see the relevance here. Whether we're using extension methods or inheritance, the goal is to change the interface to allow another method. How this method is implemented, whether by composition, generics or some other technique, is orthogonal.

like image 8
Steven Sudit Avatar answered Oct 23 '22 23:10

Steven Sudit