Monday, November 1, 2021

C# Extension method


Extension method

Let some methods are already created inside a class or structure and you want to add another new method to that class or structure. But there may be many types of limitations or constraints in adding a new method, such as:-
  1. It may happen that the source code of that class or structure is not available, so it will not be possible to create a new method within that class or structure.
  2. It may happen that that class is a sealed class due to which it will not be possible to create its child class. So new method cannot be created in child class using inheritance.
  3. It may also happen that if existing methods are in struct then struct can't inherit struct and create new struct because struct doesn't support inheritance.
  4. It may also happen that it is possible to inherit the class, however there may be a problem that if a new method is created by creating a child class, then the code will be recompiled but the purpose of the project is not to recompile the existing code.
Therefore, we see that creating a new method of a class( or struct) is not possible in many circumstances and even the project manager may be reluctant to extend the existing class even if it is possible because of side effects of the newly created method.

Keeping the above constraints in mind, if a new method is to be created for a class, then it should be created in a completely new class and this class should be bound together with the old class as if the method is created in the old class. The newly created method is called by help of old class as if the method is the member of old class, although the method is defined in a new class. Such method is called extension method.

The class of the extension method must be a static class so that the binding of the new extension method to the other class is done only at the time of loading. We know that there are only static methods within a static class.

This is the specialty of extension methods, they must have a parameter with them. This first parameter refers to the class or type with which the extension method is to be bonded. 

In other words extension methods are static methods from coding point of view whereas extension methods are non-static methods from behavioral point of view. Behaviorally, extension methods are methods of the class whose object is called by the object of the class, not the method of the static class.

Note that when compiling the source code of a class with an extension method, the binding with the old class is done at the time of class loading and no recompilation of the old code is required. In this way, only the code of the new class is compiled, and by binding the method of that new class to the old class, the new extension method can be called by the object of the old class.

To bind the method of the new class to the old class, a reference to the old class is given as the first parameter of the method of the new class, which is called the extension method, and the this keyword is used before that reference type. Is. We can understand this concept better through example.
  

using System;

namespace ExtensionMethodEx
{
    class Class1
    {
        public void Display()
        {
            Console.WriteLine("Normal Method in Class1.");
        }
    }
}

using System;

namespace ExtensionMethodEx
{
    static class Class2
    {
        public static void ExtensionMethod1(this Class1 class1)
        {
            Console.WriteLine("This extension method bound with Class1 objects.");
        }
    }
}

using System;

namespace ExtensionMethodEx
{
    class Program
    {
        static void Main(string[] args)
        {
            Class1 c1 = new Class1();
            c1.Display();
            c1.ExtensionMethod1();
            //Another approach to call
            Class2.ExtensionMethod1(c1);
            Console.ReadKey();
        }
    }
}
In C#, extension method is used to add a new method to a class. When this new method is tied to a class, then this new method can be called by the object of that class. It is not that the new method is added by coding within the class, in fact, the extension method is created inside a new static class and this new static class is bound to the class with which it has to be included. . Therefore, by extension method, it means to bind a static method of a static class with another class so that that static method can be called by an object of another class using it as a non-static method.

There can be many reasons for making a static method of a class like
  1. The source code of that class is not available, so it is not possible to create a new method within that class.
  2. It is a sealed class due to which it is not possible to create its child class
  3. It should not be a class but a structure, as you know that it is not possible to inherit the structure, so any new method of that structure is not possible through inheritance.

Edited on 5th July 2023

No comments:

Post a Comment

Hot Topics