Click here to Skip to main content
13,504,050 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


18 bookmarked
Posted 4 Apr 2011

Extension Methods in C#

, 4 Apr 2011
Rate this:
Please Sign up or sign in to vote.
This feature is fascinating. Extension Methods give us the ability to include custom methods with the existing types w/o inheriting the actual type. Gives you the ability to invoke the methods as if they are instance methods on the actual type. But at compile time, an extension method has lower priority than the instance methods. Suppose you created an extension method name IsNullOrEmpty for the type String. The method invocation will always pick the one which is in the instance and ignore your extension method.

Though they are extending the instance type with methods, these methods are never have the ability to access the private members or methods of the type.

See this example,
namespace ExtensionMethods {
  public static class Extensions {
    public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek) {
      int diff = dt.DayOfWeek - startOfWeek;
      if (diff < 0) {
        diff += 7;
      return dt.AddDays(-1 * diff).Date;

It will return the date of start of the week. You can invoke this method like this,
DateTime dt = DateTime.Now.StartOfWeek(DayOfWeek.Monday);

To define extension methods with few words we can say

- They are static methods.
- First parameter of the method must specified the type that operating on. Proceed with the ‘this’ modifier.
- Ignore first parameter during invocation.
- Call the methods as if they were instance methods on the type.


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


About the Author

Md Nazmoon Noor
Software Developer (Senior)
Bangladesh Bangladesh
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralReason for my vote of 5 simple explanation... Pin
CGN00715-Sep-11 19:34
memberCGN00715-Sep-11 19:34 
GeneralDo you know about <a href="">h... Pin
InTRUEdeR19-Apr-11 1:15
memberInTRUEdeR19-Apr-11 1:15 
GeneralExcelent review on Extension Methods. They are very handy in... Pin
Member 414818817-Apr-11 19:26
memberMember 414818817-Apr-11 19:26 
GeneralReason for my vote of 5 It is the little things that make th... Pin
Kim Togo12-Apr-11 0:24
memberKim Togo12-Apr-11 0:24 
GeneralReason for my vote of 5 Handy new feature that I was looking... Pin
Steven O11-Apr-11 18:17
memberSteven O11-Apr-11 18:17 
GeneralExtension Methods Pin
Max Peck12-Apr-11 4:20
memberMax Peck12-Apr-11 4:20 
GeneralRe: Extension Methods Pin
Ronee Noor12-Apr-11 8:12
memberRonee Noor12-Apr-11 8:12 
GeneralRe: Extension Methods Pin
Max Peck12-Apr-11 9:50
memberMax Peck12-Apr-11 9:50 

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 | Terms of Use | Mobile
Web02 | 2.8.180417.1 | Last Updated 4 Apr 2011
Article Copyright 2011 by Md Nazmoon Noor
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid