Go to DotNetFunda.com
 Online : 851 |  Welcome, Guest!   Login
 
Home > Articles > C# > Extension Method (.Net 3.5 and above)

Submit Article | Articles Home | Search Articles |

Extension Method (.Net 3.5 and above)

 Download source file
 Posted on: 6/29/2009 6:27:15 AM by Cp_raj | Views: 3093 | Category: C# | Level: Intermediate | Print Article
This article describes the use of Extension method in C#.

.NET Training Videos!
Buy online comprehensive training video pack just for $35.00 only, see what's inside it.

Introduction
Extension method is new to C#.Its allow you to add new method to the existing class.
Description

1.Extension method allows you to add new method to the existing class without modifying the code, recompiling or modifying the original code.
2.Extension method are special kind of static method but they are called using instance method syntax. Their first parameter specify which type the method operate on, and the parameter is precede by  “this” modifier.
3.Extension method are only in scope when you explicitly import the namespace into your source code with “using” directive.
For Example in string object there is no method call “Reverse()”  function .But if you need to extend the string ….

Sample Code


C# Code

namespace ExtensionMethods
{
    public static class ExtensionsClass
    {
        public static string Reverse(this String strReverse)
        {
            char[] charArray = new char[strReverse.Length];
     int len = strReverse.Length - 1;
         for (int i = 0; i <= len; i++)
     {  
        charArray[i] = strReverse[len-i];
     }
     return new string(charArray);
        }
    }   
}

C# Code

How to use?

string s = "Hello Extension Methods";
string strReverse = s.Reverse ();


Here "s" is nothing but the parameter of Reverse() method.

Conclusion

Extension method is nothing but the Visitor pattern.

Visitor Pattern:-
Visitor pattern allows us to change the class structure without changing the actual class. Its way of separating the logic and algorithm from the current data structure. Due to this you can add new logic to the current data structure without altering the structure. Second you can alter the structure without touching the logic



If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Interesting?   Share and Bookmark this kick it on DotNetKicks.com


Experience:8 year(s)
Home page:http://www.dotnetfunda.com
Member since:Tuesday, May 26, 2009
Level:Starter
Status: [Member]
Biography:I started my career with Asp,ms-access,sql server 7,xml.Then i updated to .net,share point server,Basics about Biztalk server
 Latest post(s) from Cp_raj

   ◘ Best Practise in String Methods posted on 7/6/2009 5:45:52 AM
   ◘ Extension Method (.Net 3.5 and above) posted on 6/29/2009 6:27:15 AM


Submit Article

About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)