Extension Method in C# | Extension Method using static

Extension Method in C#

Hello Folks, in this post we will try to understand Extension Method in C# along with example.

I have already explained  Reflection Introduction c#Insert data into Database table using SQLBulkCopy class in C#as keyword C#, Import/Upload Excel file in asp.net with C#The Microsoft Jet database engine cannot open the file. It is already opened exclusively by another user,   Call method after specific time interval C#Alert from code behind asp.net,required field validator in asp.net,Difference between RegisterClientScriptBlock and RegisterStartupScript asp.netDifference between ref and out parameters.

An Extension method is used to extend existing class, the method must be static.

To define Extension method, we have to follow below steps:

  1. Define class with Static
  2. Define Method using static and method’s first parameter followed by “this” keyword.

Lets have a look into how could we create and consume Extension Method in C# by example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExtensionMethod
{
    class Program
    {
        static void Main(string[] args)
        {
            string strInput = "extension method in c#- Technothirsty";
            string strOutPut = strInput.strFirstLatterUpper();
            Console.WriteLine("Input String  : " + strInput);
            Console.WriteLine("Output String : " + strOutPut);
            Console.ReadKey();
        }
    }

    public static class MyExtensionClass
    {
        public static string strFirstLatterUpper(this string strVal)
        {
            if (strVal.Length > 0)
            {
                char[] array = strVal.ToCharArray();
                array[0] = char.ToUpper(array[0]);
                return new string(array);
            }
            return strVal;
        }
    }
}

OUTPUT:

Extension Method in C#
Extension Method in C#

As explain in example, we have declared “MyExtensionClass” static class in which we have declared static method, which will be used as extension method of string.  We are extending C# string to convert first latter of string in Capital.

You have noticed in code that inside method, I have declared parameter with “this” keyword and declare method with static. By this we could directly use “strFirstLatterUpper” method directly as Extension method of string. We can call that method directly as shown below:

string strOutPut = strInput.strFirstLatterUpper();
 
Advantages of Extension Method in C#?
  1. Re-usability of code
  2. Maintainable code
  3. Clean and readable code in LINQ query.
Disadvantages of Extension Method in C#?
  1. There is no compilation error or warning, if you would have Extension method and Regular method with same class or context.

* Difference between a regular static method and an extension method is that the first parameter of the extension method specifies the type that it is going to operator on, preceded by the this keyword.

* Extension methods can be used anywhere in the application by including the namespace of the extension method

Leave a Reply