Without changing, deriving, or recompiling the original class, structure, or interface, you can inject extra methods using extension methods. You can enhance your own custom class with extension methods,

 

The extension method will be available throughout the application by including the namespace in which it has been defined.

A static class defines a specific type of method called an extension method. Establishing a static class is the initial step in defining an extension method. 

define a static method as an extension method where the first parameter of the extension method specifies the type on which the extension method is applicable.

 

namespace ExtensionMethods{
   public static class IntExtensions    {
       public static bool IsGreaterThan(this int i, int value)     {
           return i > value;
       }
   }
}

 

int i = 10;
bool result = i.IsGreaterThan(100); 
Console.WriteLine(result);

An extension method is different from a normal static method only in that the first parameter of an extension method, which comes before the this keyword, tells the method what type it acts on.

 


Related Question