Click here to Skip to main content
Click here to Skip to main content

Tagged as

Extension Methods

, 11 Nov 2010
Rate this:
Please Sign up or sign in to vote.
Extension Methods

 

What is Extesion Methods ?

 

Method allow programmer to "add" methods to existing types without creating a new derived type, recompiling, or by modifying the original type. Methods are static methods they are called as if they were instance methods on the extended type.





Example : 




 
public static class Utilities
{
   public static string encryptString(this string str)
  {
      System.Security.Cryptography.MD5CryptoServiceProvider x = new                      System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] data = System.Text.Encoding.ASCII.GetBytes(str);
            data = x.ComputeHash(data);
            return System.Text.Encoding.ASCII.GetString(data);
    }
}
 




How to call Extension Method ?



As you can see in below image IDE intelligence shows the extension method with the down arrow when you want to call on the given datatype.  

 

 
As you can see in above example I have created a function encryptString to encrypt the string which is having sensitive data and want to store its in encrypted form in database.
 
So the common use of extenstion method is when you are creating utility functions in your project which can extend functionality of the existing .net type or of the existing dll available in your project.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India

Microsoft C# MVP (12-13)

 
Hey, I am Pranay Rana, working as a ITA in MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.
 
For me def. of programming is : Programming is something that you do once and that get used by multiple for many years
 

You can visit my blog

StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr
 
Awards:


Follow on   Twitter   LinkedIn

Comments and Discussions

 
GeneralReason for my vote of 4 good screen shot and clean explanati... PinmemberMember 345669117-Nov-10 0:54 
GeneralReason for my vote of 3 Grammar. Only one example. PinmemberOshtri Deka15-Nov-10 2:53 
GeneralIntelligence vs. IntelliSense Pinmemberjohannesnestler19-Nov-10 3:25 
AnswerRe: Intelligence vs. IntelliSense PinmemberRajesh Ganapathy Sankar21-Jul-11 19:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 12 Nov 2010
Article Copyright 2010 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid