How to Use Extension Methods in C#

Extension Methods are a very popular topic in C# that increases the functionality of any type that is often used in the entire application or in terms of single use also. In Visual Studio, when we press the period (".", also known as "dot") after any data type variable, it gives all the methods for that data type, as in the following image:

extensionmethod1.png
Now If we need to add one more custom function for that data type variable then Extension Methods can be used here as a Superman for us.

The best example we can see is for a Phone Number Format, the user can enter 10 digit numbers in a text box and we need to save it in a different format, for example US Phone Number Format (xxx)-(xxxx)-xxx.

First create a static class with a static method. An Extension Method must be static. Here I have posted a code example for an Extension Method of String type. This example converts a normal Phone Number in US format. You can write here your own code for any functionality. The main point is the "this" keyword in the method "USPhoneNumberFormat(this string value)", it will fetch a value from the current string object.

After creating an Extension Method, just build the application and see your method by pressing the period (".", also known as "dot") with a variable as in the following image:

extensionmethod2.png
Class

public static class ExtensionMethods

{

    public static string USPhoneNumberFormat(this string value)

    {

        if (value.Length == 10)

        {

            StringBuilder phoneNumber = new StringBuilder();

 

            for (int i = 0; i < 10; i++)

            {

                if (i == 0)

                {

                    phoneNumber.Append("(");

                    phoneNumber.Append(value[i]);

                }

                else if (i == 3)

                {

                    phoneNumber.Append(")-(");

                    phoneNumber.Append(value[i]);

                }

                else if (i == 7)

                {

                    phoneNumber.Append(")-");

                    phoneNumber.Append(value[i]);

                }

                else

                {

                    phoneNumber.Append(value[i]);

                }

            }

            return phoneNumber.ToString();

        }

        return value;

    }

}

default.aspx.cs page

protected void Page_Load(object sender, EventArgs e)

{

    string value = "1234560789";

    value = value.USPhoneNumberFormat();

}

Passing parameter in Extension Method

In the above example, we have seen that by specifying "this" in the method argument, it captures the value of the current object. Now we will see how to pass parameters in Extension Methods.

In an Extension Method, the first parameter must be the data type of the variable as in the following.

String type


For string type:

public static string USPhoneNumberFormat(this string value)

{

    return "";

}

Integer type


For integer type:

public static int USPhoneNumberFormat(this int value)

{

    return "";

}

Now suppose we need to pass one more parameter of string type in any type of methods, that can be done as in the following:

public static string USPhoneNumberFormat(this string value, string arg)

{

    return "";

}

So in this way, we can overload the Extension Method.
 

protected void Page_Load(object sender, EventArgs e)

{

    string value = "1234560789";

 

    //no argument

    value = value.USPhoneNumberFormat();

 

    //passing one arguments

    value = value.USPhoneNumberFormat("pass here what you want");

}