Extension Methods in C#

Extension Methods in C#

In this article, I am going to discuss the Extension Methods in C# with an example.

It is a new feature that has been added in C# 3.0 which allows us to add new methods into a class without editing the source code of the class i.e. if a class consists of set of members in it and in the future if we want to add new methods into the class, we can add those methods without making any changes to the source code of the class.

Extension methods can be used as an approach of extending the functionality of a class in the future if the source code of the class is not available or we don’t have any permission in making changes to the class.

Before extension methods, inheritance is an approach that used for the extending the functionality of a class i.e. if we want to add any new members into an existing class without making a modification to the class, we will define a child class to that existing class and then we add new members in the child class.

In case of an extension method, we will extend the functionality of a class by defining the methods, we want to add into the class in a new class and then bind them to an existing class.

Both these approaches can be used for extending the functionalities of an existing class whereas, in inheritance, we call the method defined in the old and new classes by using object of the new class whereas, in case of extension methods, we call the old and new methods by using object of the old class.

Lets us see an example for better understanding

Add a class OldClass.CS and write the following code

public class OldClass
{
    public int x = 100;
    public void Test1()
    {
        Console.WriteLine("Method one: " + this.x);
    }
    public void Test2()
    {
        Console.WriteLine("Method two: " + this.x);
    }
}

Now our requirement is to add three new methods to the class OldClass. To do this add a class NewClass.CS and write the following code

public static class NewClass
{
    public static void Text3(this OldClass O)
    {
        Console.WriteLine("Method Three");
    }
    public static void Text4(this OldClass O, int x)
    {
        Console.WriteLine("Method Four: " + x);
    }
    public static void Text5(this OldClass O)
    {
        Console.WriteLine("Method Five:" + O.x);
    }
}

Now to test whether the methods are accessed using the old class objects or not, add a class Program.CS and write the following code

public class Program
{
    static void Main(string[] args)
    {
        OldClass obj = new OldClass();
        obj.Test1();
        obj.Test2();

        //Calling exrension methods
        obj.Text3();
        obj.Text4(10);
        obj.Text5();

        Console.ReadLine();
    }
}

Now, run the application and see everything is working as expected and it will display the following output.

Extension Methods in C#

Points to Remember while working with Extension methods:
  1. Extension methods must be defined only under the static class.
  2. As an extension method is defined under a static class, compulsory that method should be defined as static only whereas once the method is bound with another class, the method changes into non-static.
  3. The first parameter of an extension method is known as the binding parameter which should be the name of the class to which the method has to be bound and the binding parameter should be prefixed with this keyword.
  4. An extension method can have only one binding parameter and that should be defined in the first place of the parameter list.
  5. If required, an extension method can be defined with normal parameter also starting from the second place of the parameter list.
SUMMARY:

In this article, I try to explain Extension Methods in C# with an example. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

1 thought on “Extension Methods in C#”

Leave a Reply

Your email address will not be published. Required fields are marked *