In this article we will look into how to use Extension methods in a very simpler way and this will be very helpful to those who have never used extension methods in their projects. With extension methods you can attach additional functionalities to an existing type.
This article is aimed at beginners to demonstrate how to use Extension methods in a very simpler
way and this will be very helpful to those who has never ever used extension
methods in their projects. With extension methods you can attach additional
functionalities to an existing type.
I’m sure after reading this article, you can
easily go with extension method. Hope this article is explained well and readers will like it.
Please give your suggestions and feedback.
Please download the attached project for better understanding.
Extension Method?Extension method is a special kind of static method that
can be associated with a type, so that it can be called as if it were an
instance method of the type.
Extension methods are defined as static methods but are called by using
instance method syntax. Their first parameter specifies which type the method
operates on, and the parameter is preceded by the ‘this
modifier. Extension methods are only in scope when you explicitly import the
namespace into your source code with a using directive.
How to define an Extension Method?See, the below example where an extension method is defined for
Here, to understand extension method 2 methods are defined. one is for
decrypt the text and another one is for encrypt the text.
The method has to be a static method in a static class. The first parameter to
the method has to be qualified using this and
this first parameter indicates the types on which this extension method
can be applied.
Importantly note that even though Decrypt and Encrypt is not a member of String, but using extension methods we have explore
Review the attached project for more and better understanding.
Things to be remember before using
Now, there are few
points should be remember before implementing extension methods:
=>First of which is that the extension methods can only access the public properties
of the type
=>The extension method signature should not be same as an existing method of the
=>The extension method for a type can only be used if the namespace that contains
the extension method is in scope.
=>In case we define extension methods in such a way that it overloads an existing
method of the original type and the calls are getting ambiguous then
the overload resolution rule will always choose the instance method over the
=>If there is some ambiguity between two extension methods then the method
containing more specific arguments will get called.
Please keep the following points in your mind before using extension methods.
One of the .NET feature LINQ uses extension methods heavily and I recommend to look how LINQ used extension methods.
This article is meant
for the absolute beginners who are not aware of these features of C#. I hope
this has been informative. Please give your feedback and suggestions.
ReferenceThere are so many
online reference available for extension methods, to explore more and more, visit this MSDN link