Open In App

Extension Method in C#

In C#, the extension method concept allows you to add new methods in the existing class or in the structure without modifying the source code of the original type and you do not require any kind of special permission from the original type and there is no need to re-compile the original type. It is introduced in C# 3.0.

Let us discuss this concept with the help of an example. Suppose you have a class or a structure that contains three methods and you want to add two new methods in this class or structure, you do not have the source code of the class/structure or do not have permissions from the class/structure, or the class is a sealed class, but you still want to add new methods in it, then you can use the concept extension method to add the new method in the existing class/structure.

Now you create a new class that is static and contains the two methods that you want to add to the existing class, now bind this class with the existing class. After binding you will see the existing class can access the two newly added methods. As shown in the below program.

Example:

First we create a class named as Geek in Program1.csfile. It contains three methods that is M1(), M2(), and M3().

C#
// C# program to illustrate the concept 
// of the extension methods
using System;
 
namespace ExtensionMethod {
 
// Here Geek class contains three methods
// Now we want to add two more new methods in it 
// Without re-compiling this class
class Geek {
 
  // Method 1
  public void M1() 
  {
      Console.WriteLine("Method Name: M1");
  }
 
  // Method 2
  public void M2()
  {
      Console.WriteLine("Method Name: M2");
  }
 
  // Method 3
  public void M3()
  {
      Console.WriteLine("Method Name: M3");
  }
  
 }
   
}

Now we create a static class named as NewMethodClass in Program2.cs file. It contains two methods that are M4() and M5(). Now we want to add these methods in Geek class, so we use the binding parameter to bind these methods with Geek class. After that, we create another named as GFG in which Geek class access all the five methods.

C#
// C# program to illustrate the concept
// of the extension methods
using System;

namespace ExtensionMethod {

// This class contains M4 and M5 method
// Which we want to add in Geek class.
// NewMethodClass is a static class
static class NewMethodClass {

    // Method 4
    public static void M4(this Geek g)
    {
        Console.WriteLine("Method Name: M4");
    }

    // Method 5
    public static void M5(this Geek g, string str)
    {
        Console.WriteLine(str);
    }
}

// Now we create a new class in which
// Geek class access all the five methods
public class GFG {

    // Main Method
    public static void Main(string[] args)
    {
        Geek g = new Geek();
        g.M1();
        g.M2();
        g.M3();
        g.M4();
        g.M5("Method Name: M5");
    }
}
}

Output:

Method Name: M1
Method Name: M2
Method Name: M3
Method Name: M4
Method Name: M5

Important Points:

Advantages:

Article Tags :
C#