A Beginner's Tutorial on Extension Methods

lakhansin-22735
Posted by in C# category on for Beginner level | Points: 250 | Views : 3469 red flag

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.


 Download source code for A Beginner's Tutorial on Extension Methods

Introduction

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 the System.String class.
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 its functionalities.


Review the attached project for more and better understanding.

Things to be remember before using Extension methods

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 type.
=>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 extension method.
=>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.

Conclusion

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.

Reference

There are so many online reference available for extension methods, to explore more and more, visit this MSDN link
http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx

Page copy protected against web site content infringement by Copyscape

About the Author

lakhansin-22735
Full Name: Lakhan Singh
Member Level: Starter
Member Status: Member
Member Since: 6/21/2013 3:50:19 AM
Country: India
Lakhan Singh Tech Lead BeyondKey System Pvt. Ltd. Indore, M.P. India
http://www.dotnetfunda.com
Technical Lead

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)