Extension method is a new feature added in c# 3.0. An Extension method is used to add new behaviors to base type without changing the base feature. Extension methods can be attached to any other existing class without changing that particular class. Using extension method you can add a method of existing type or modify the original. Basically Extension methods are static methods.

Key Points of Extension Method

1.  Extension method is a static method that must be exists in static class.

2.  It always uses “this” keyword with first parameter in the parameter list. Basically this keyword tells to the compiler that what class you have to extend. You can also other parameter proceeding with it.

3.  You cannot override base method of existing type using an extension method. If you do this compiler will not give you an error but it will not work.

4.  If you create an extension method with same name as base method but different parameters then the extension method will call first not base method.

5.  If you create an extension method with same name and same signature as an instance method has then extension method will never be called because of compiler take this as low priority method.

6.  You can not use an extension method with properties, events or fields.

7.  If an extension method with same name and signature exist in two or more than two different namespace and these namespace are included with a class where you want to use extension method then compiler doesn’t give any error. But it will give an error when you will try to call one of them extension method.

8.  Extension method’s namespace should be included when you use. You can import namespace of the class to use “Using” statement.

Example I
public static class StringHelpers
{

        public static string[] GetUserFirstAndLastName(this string name)
        {
            var firstName = string.Empty;
            var lastName = string.Empty;            
            var len = name.Split(' ').Length;

            string[] names = name.ToString().Trim().Split(new char[] { ' ' }, len);
            if (names.Length == 1)
            {
                firstName = names[0];
                lastName = "";
            }
            else if (names.Length == 2)
            {
                firstName = names[0];
                lastName = names[1];
            }
            else
            {
                firstName = names[0];
                lastName = names[names.Length - 1];
            }
            string[] firstlastName={(firstName),(lastName)};
            return firstlastName;
        }
}

In this example I have created an extension method which return a string array with firstname and lastname from pass name. below I am going to show how to use this method.

var name="Mukesh Kumar Singh";

//GetUserFirstAndLastName is an extension method.
string[] firstlastName = name.GetUserFirstAndLastName();

var FirstName = firstlastName[0].ToString();
var LastName = firstlastName[1].ToString();
Example II
public static class StringExtensionMethod
{
    public static string[] SplitByComma(this String s)
    {
       return s.Split(',');
    }
}
class Program
{
    static void Main(string[] args)
    {
      string input = "string1,string2,string3";
      var result = input.SplitByComma();
      Console.WriteLine(result[1]);
    }
}
Conclusion:

So, Today we learned what is an extension method in C# and how to use it. We also learned the main key points about an extension method.

I hope this post will help you. Please put your feedback using comment which helps me to improve myself for next post. If you have any doubts please ask your doubts or query in the comment section and If you like this post, please share it with your friends. Thanks