Tag Archives: extension-methods

What are extension methods? How/When to use them?

Extension methods are methods that can be created extending existing types without the need to inherit from a class and creating your own custom logic. It can also be applied on interfaces.

2015-47-15 05-47-11-407

From MSDN:

Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.

That means we can extend methods from any type, even types where we don’t have access to the source code.

Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.

We build extension methods by declaring it as a static method, but there is a little tweak makes it compile into a method that can be called from an instance.
Continue reading

Advertisements

An overview of Extension Methods and a handful set of them

Extension methods are methods that you can add to instances without necessarily having access to the class source.

Linq methods were implemented as Extension Methods.

This is really useful when you need to apply some common complex action on top of some object. I personally think you should replace any Util class you might have to extension methods, it looks more semantically correct.
Continue reading