Click here to Skip to main content
13,592,724 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 11 Nov 2010
Licenced CPOL

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.


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


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 -
My CV :-


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    Praise Praise    Rant Rant    Admin Admin   

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

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03-2016 | 2.8.180618.1 | Last Updated 12 Nov 2010
Article Copyright 2010 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid