How To Use Extension Methods In C#

Introduction

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.

extensionmethod

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.

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 0; // You should return an integer value, not an empty string.
}

Now suppose we need to pass one more parameter of string type in any type of method; 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 argument
    value = value.USPhoneNumberFormat("pass here what you want");
}


Recommended Free Ebook
Similar Articles