Go to DotNetFunda.com
 Online : 1486 |  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)

1 vote(s)
Rating: 5 out of 5
red flag  Posted on: 6/29/2009 6:27:15 AM by Cp_raj | Views: 6880 | Category: C# | Level: Intermediate


This article describes the use of Extension method in C#.

Download


 Download source code for Extension Method (.Net 3.5 and above)



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.

Found interesting? Add this to:

| More



Please Sign In to vote for this post.

 
Latest post(s) from Cp_raj

Latest Articles
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

Submit Article

About Us | The Team | Advertise | Contact Us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 9/3/2010 4:37:25 AM