Pages - Menu

Monday, 23 May 2016

Extension method in c#

What are extension methods?

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

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

When do you use extension methods, ext. methods vs. inheritance?


Extension methods allow existing classes to be extended without relying on inheritance or having to change the class's source code. This means that if you want to add some methods into the existing String class you can do it quite easily. 

Here's a couple of rules to consider when deciding on whether or not to use extension methods:

Extension methods cannot be used to override existing methods
An extension method with the same name and signature as an instance method will not be called
The concept of extension methods cannot be applied to fields, properties or events
Use extension methods sparingly....overuse can be a bad thing!

Times to use extension methods:

when you don't control the types being extended
where you don't want to force the implementor to provide code that can be done using the existing methods
For an example of the second point; you might have an extension method on IList<T> (for example, Sort) that can be written entirely using the existing IList<T> members... so why force anybody else to write anything? This is the foundation block of LINQ, and allowed Microsoft to provide much more functionality without breaking anything.

Times to not use extension methods:

when polymorphism is critical; you cannot guarantee that your code will be the version that gets executed with an extension method, as methods directly on the type take precedence
when you need access to private/protected members

How to use extension methods?

An extension method is a static method of a static class, where the "this" modifier is applied to the first parameter. The type of the first parameter will be the type that is extended.

Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.


Benefits of extension methods

Extension methods allow existing classes to be extended without relying on inheritance or having to change the class's source code.
If the class is sealed than there in no concept of extending its functionality. For this a new concept is introduced, in other words extension methods.
This feature is important for all developers, especially if you would like to use the dynamism of the C# enhancements in your class's design.


Important points for the use of extension methods:

An extension method must be defined in a top-level static class.
An extension method with the same name and signature as an instance method will not be called.
Extension methods cannot be used to override existing methods.
The concept of extension methods cannot be applied to fields, properties or events.
Overuse of extension methods is not a good style of programming


No comments:

Post a Comment