Click here to Skip to main content
11,639,554 members (64,602 online)
Click here to Skip to main content

Tagged as

Extension Methods

, 11 Nov 2010 CPOL 7.3K 2
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:



You may also be interested in...

Comments and Discussions

 
GeneralReason for my vote of 4 good screen shot and clean explanati... Pin
Member 345669117-Nov-10 0:54
memberMember 345669117-Nov-10 0:54 
GeneralReason for my vote of 3 Grammar. Only one example. Pin
Oshtri Deka15-Nov-10 2:53
memberOshtri Deka15-Nov-10 2:53 
GeneralIntelligence vs. IntelliSense Pin
johannesnestler19-Nov-10 3:25
memberjohannesnestler19-Nov-10 3:25 
AnswerRe: Intelligence vs. IntelliSense Pin
Rajesh Ganapathy Sankar21-Jul-11 19:12
memberRajesh 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 | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 12 Nov 2010
Article Copyright 2010 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid