Understand Extension Method in C#

This article explains extension methods of C#. The extension method is a modified version/slightly changed version of a static method.

Extension methods are one of the beautiful and less famous by its name features in the C# language. Actually many of us have used this method but most of the time unknowingly.
 
An extension method has a simplified calling syntax. We can say that it's a modified version/slightly changed version of a static method. There are a few syntaxes of extension methods.
  • Extension method must be defined as a static function.
  • The first parameter of the extension method must be declared with the “this” keyword.
The first point says that an extension method must be defined as a static function but the method will behave as an instance method and this is the beauty of extension methods. The next point says that the first parameter of the extension method will be defined with the “this” keyword.
 
Let's have a look at the following example. Here we are trying to arrange an array using extension methods of the .Net library function.
 
 
 
Here we see that OrderBy() is an extension method and in Visual Studio an extension method is represented by a down arrow.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using ClassLibrary;  
  7. namespace ConsoleAPP  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             int[] data = {1,5,3,2,9,7 };  
  14.             var arranged_data = data.OrderBy(t=>t);  
  15.             foreach (var i in arranged_data)  
  16.             Console.Write(i);  
  17.             Console.ReadLine();  
  18.         }  
  19.     }  
  20. }  
And the output is here.
 
 
 
Cool! We can implement our own extension method as in the following. In this example we have defined one static function called myExtension and the class contains the myToUpper() function that is another static function.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using ClassLibrary;  
  7. namespace ConsoleAPP  
  8. {  
  9.     public static class myExtension  
  10.     {  
  11.         public static string myToUpper(this string str)  
  12.         {  
  13.             return str.ToUpper();  
  14.         }  
  15.    }  
  16.     class Program  
  17.     {  
  18.         static void Main(string[] args)  
  19.         {  
  20.             string name = "sourav kayal";  
  21.             Console.WriteLine(name.myToUpper());  
  22.             Console.ReadLine();  
  23.         }  
  24.     }  
  25. }  
Just notice that the first argument of the function is defined with this keyword as per rule. And we can use the method associated with a string because we saw that the first argument is a string type. If it was some other data type then we would allow use with the specific data type. Here is sample output.
 
 
 
Fine, let's try another example.
  1. public static class myExtension  
  2. {  
  3.    public static int myIntHandle(this int val,int addVal)  
  4.    {  
  5.       return val + addVal;  
  6.    }  
  7. }  
  8. class Program  
  9. {  
  10.    static void Main(string[] args)  
  11.    {  
  12.       int value = 100;  
  13.       Console.WriteLine(value.myIntHandle(100));  
  14.       Console.ReadLine();  
  15.    }  
  16. }  
Here, the extension method is defined for an integer type and it takes two arguments. Notice that the first argument is defined with the “this” keyword and the second argument is not. Here is the output of the example.
 
 
 
Finally
 
Extension methods are easy and handy to use. Most of the functions in the .NET class library uses extension methods with a lambda expression. Happy learning.