Extension Methods in C#

Extension method in C-sharp is a concept that allows programmers to add new methods in the existing class or the structure. This is done without modifying the source code. Also, the programmer does not require initial or special permission from the original source code and there is no need to re-compile. Extension method was first introduced in C# 3.0

To help you understand this concept better, we have to introduce an example: Assume that we have a structure or class with three methods and we want to add two new methods to this class or structure. However, we do not either have the source code of the structure or class, or the permission from it. But we still want to add these two methods even though the class is a sealed class. We can use the extension method in C-sharp to do this.

First, create a static new class that contains the two methods which you want to add to the existing class/structure. You will see that the existing class has access to the two new added methods after binding.

Example in program:

Create a class named Helper in Program.cs file. It should contain three methods, namely m1(), m2() and m3().

// C# program demonstrating the concept of extension methods

using system;

namespaceExtensionMethod {

//Our helper class contains 3 methods

//We want to add two more methods to it without compiling the class

Class Helper {

//Method 1

public void  M1 ()

{

Console.WriteLine (“Name of method: M1”);

}

//Method 2

public void M2 ()

{

Console.WriteLine (“Name of method: M2”);

}

//Method 3

public void M 3 ()

{

Console.WriteLine (“Name of method: M3”);

}

}

}

Next, we create a static class in program2.cs. Let us name it NewMethodClass. It should contain the two new methods that we want to add. That is, M4 () and M5 (). We now use the binding parameter to add these two methods to our Helper class. Then, we will create another class named PAH in which Helper class can access all the five methods.

// C# program demonstrating the concept of extension methods

using system;

namespaceExtensionMethod {

//This new class should contain the two new methods M4 and M5 which we want to add to helper class

//NewMethodClass is a static class

Static class NewMethodClass {

//Method 4

Public static void m4 (this Helper g)

{

Console,WriteLine (“The method name is: M4”);

}

//Method 5

Public static void m5 (this Helper g, String str)

{

Console,WriteLine (“str”);

}

}

//Next, we create a class  in which our Helper class can access all the five methods

Public class PAH {

//The main method

Public static void Main (string [ ] args)

{

Helper g = new Helper ();

g.M1 ();

g.M2 ();

g.M3 ();

g.M4 ();

g.M5 ( “The method name is : M5”);

}

When we run this program, the output will be:

The method name is: M1

The method name is: M2

The method name is: M3

The method name is: M4

The method name is: M5

Important points to note in extension methods

  • Binding parameters are used to bind a new method to an existing class or structure. These parameters are only used for binding and not any other use. As a result, they do not take any value when you call the extension method. The binding parameter is always present in the first place in the parameter list of the extension method. The compiler will return an error if you write it in the second, third or any other place rather than the first place. To create a parameter, we use the “this” keyword, followed by the name of the class you want to add and then the parameter name. For example

this Helper h

Where:

  • this – is the parameter keyword used for binding
  • Helper – the name of the class we want to add
  • h – parameter name
  • Although extension methods are always defined as a static method, they will convert into non-static methods since they are bound with any structure or class.
  • The compiler will print the existing method if an extension method is defined with the same name and the existing method. In simple terms, we are trying to say that the extension method in C# does not support method overriding
  • The extension method allows us to add new methods in the sealed class

Advantages of extension method

  • It can also work with a sealed class
  • It allows you to add new methods in an existing class without using inheritance
  • You do not have to modify the source code of an existing class  to add new methods to it