Click here to Skip to main content
13,866,046 members
Click here to Skip to main content
Add your own
alternative version


49 bookmarked
Posted 17 Mar 2009
Licenced CPOL

Extension Methods in C#

, 17 Mar 2009
Rate this:
Please Sign up or sign in to vote.
Extension methods as one of C# Enhancements


In this article, I would like to introduce one of the new C# 3.0 enhancements “Extension Methods” and so on. This feature is available in C # 3.0 compilers and further versions (C# 4.0); this feature is very important for all developers especially if you would like to use the dynamism of the C# enhancements to take place in your class design.

Why Do We Need Extension Methods?

While designing your classes in .NET projects, we used to have sibling classes which do simple operation from its parent classes to add a kind of customization for parents’ methods. Suppose that you want to extend int class in .NET by adding a factorial method to it using recursion technique.

One way of thinking is to inherit int class and add your new method to it and use your new methods in your code. This is a valid solution but let us see what extension methods can provide us.

Using extension methods, you can use your new methods as a part of int class in .NET.  

In the above screen shot, I wrote the following line of code:

int x = 3;

Factorial method becomes a part of int class by implementing factorial method as an extension method in my class without inheriting int class in .NET, see below (Figure 1.1).  

Figure 1.1 of extension method

What’s the Specification of an Extension Method?

An extension method is a special kind of static method that allows you to add new methods to existing types without creating derived types.

The extension methods are called as if they were instance methods from the extended type, For example: x is an object from int class and we called it as an instance method in int class.

How to Create my Extension Method?

Simply, you create your own static method in your class and put this keyword in front of the first parameter in this method (the type that will be extended).

public static class MyMathExtension 
    public static int factorial(this int x) 
        if (x <= 1) return 1; 
        if (x == 2) return 2; 
            return x * factorial(x - 1); 

Complete Code for My Demo

namespace ConsoleApplication1 
    class Program 
        static void Main(string[] args) 
            int x = 3; 
    public static class MyMathExtension 
        public static int factorial(this int x) 
            if (x <= 1) return 1; 
            if (x == 2) return 2; 
                return x * factorial(x - 1); 

General Tips in Extension Methods Usage

This section is optional reading section for understanding the core idea of extension methods:

  1. This keyword has to be the first parameter in the extension method parameter list.
  2. Extension methods are used extensively in C# 3.0 and further version specially for LINQ extensions in C#, for example:
    int[] ints = { 10, 45, 15, 39, 21, 26 };
    var result = ints.OrderBy(g => g);

    Here, order by is a sample for extension method from an instance of IEnumerable<T> interface, the expression inside the parenthesis is a lambda expression.

  3. It is important to note that extension methods can't access the private methods in the extended type.
  4. If you want to add new methods to a type, you don't have the source code for it, the ideal solution is to use and implement extension methods of that type.
  5. If you create extension methods that have the same signature methods inside the type you are extending, then the extension methods will never be called.


In this article, I have stated the use and the benefits we will gain by using extension methods in C# which is available starting from C# 3.0 and further versions of C#.

It’s important to make note of the tips I have mentioned in the article while you implement your extension methods.


  • 17th March, 2009: Initial post


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


About the Author

Moustafa Arafa
Software Developer
Kuwait Kuwait
Moustafa Arafa
Dotnet Boom Supportive Development Manager

You may also be interested in...

Comments and Discussions

GeneralMy vote of 2 Pin
Er Ayush Gupta28-Jul-14 5:38
professionalEr Ayush Gupta28-Jul-14 5:38 
Questionvery simple language Pin
saiprasad228-Mar-13 23:46
membersaiprasad228-Mar-13 23:46 
GeneralMy vote of 3 Pin
Member 95526328-Jan-13 2:52
memberMember 95526328-Jan-13 2:52 
GeneralMy vote of 5 Pin
jinzai19-Nov-12 13:36
memberjinzai19-Nov-12 13:36 
GeneralMy vote of 3 Pin
vishnupasuleti8-Oct-12 7:58
membervishnupasuleti8-Oct-12 7:58 
GeneralFactorial function Pin
Ramesh Karkala2-Feb-12 6:09
memberRamesh Karkala2-Feb-12 6:09 
GeneralRe: Factorial function Pin
  Forogar  11-May-12 5:23
professional  Forogar  11-May-12 5:23 
GeneralMy vote of 3 Pin
secorbett1-Mar-11 3:22
membersecorbett1-Mar-11 3:22 
GeneralThanks Pin
mohamedenew8-Aug-10 2:15
membermohamedenew8-Aug-10 2:15 
GeneralMy vote of 2 Pin
Joe Enos17-Mar-09 11:26
memberJoe Enos17-Mar-09 11:26 
GeneralRe: My vote of 2 Pin
Moustafa Arafa17-Mar-09 13:20
memberMoustafa Arafa17-Mar-09 13:20 
GeneralMy vote of 2 Pin
Patric_J17-Mar-09 11:15
memberPatric_J17-Mar-09 11:15 
GeneralMy vote of 2 Pin
Adrian Pasik17-Mar-09 10:40
memberAdrian Pasik17-Mar-09 10:40 
GeneralRe: My vote of 2 Pin
Moustafa Arafa17-Mar-09 10:54
memberMoustafa Arafa17-Mar-09 10:54 

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
Web04 | 2.8.190214.1 | Last Updated 17 Mar 2009
Article Copyright 2009 by Moustafa Arafa
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid