Understand Extension Method in C#

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.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using ClassLibrary;

 

namespace ConsoleAPP

{

    class Program

    {

        static void Main(string[] args)

        {

            int[] data = {1,5,3,2,9,7 };

            var arranged_data = data.OrderBy(t=>t);

            foreach (var i in arranged_data)

                Console.Write(i);

 

            Console.ReadLine();

        }

    }

}


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.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using ClassLibrary;

 

namespace ConsoleAPP

{

    public static class myExtension

    {

        public static string myToUpper(this string str)

        {

            return str.ToUpper();

        }

   }

    class Program

    {

        static void Main(string[] args)

        {

            string name = "sourav kayal";

            Console.WriteLine(name.myToUpper());

            Console.ReadLine();

        }

    }

}


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.

public static class myExtension
{
   public static int myIntHandle(this int val,int addVal)
   {
      return val + addVal;
   }
}
class Program
{
   static void Main(string[] args)
   {
      int value = 100;
      Console.WriteLine(value.myIntHandle(100));
      Console.ReadLine();
   }
}

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.

Up Next
    Ebook Download
    View all
    Learn
    View all