C# Extension Methods

In c#, extension methods are useful to extend the behaviour of existing types by adding new methods without modifying, deriving or recompiling the original types such as class, struct or interface.

 

In c#, we can implement our own extension methods for any .NET type such as custom class, .NET framework class or third party classes.

 

To create an extension method, we need to define a static class and implement an extension method as a static method with the required parameters. The first parameter of the method will specify the type on which the extension method will operate on and it must be preceded by this modifier.

 

Following is the example of implementing an extension method for System.string class.

 

using System.Text.RegularExpressions;

 

namespace Extensions

{

    public static class StringExtension

    {

        // Extension Method for string type

        public static bool IsValidEmail(this string str)

        {

            var result = Regex.IsMatch(str, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");

            return result;

        }

    }

}

If you observe above example, we are extending the behaviour of string type by creating IsValidEmail extension method.

 

In c#, the extension methods are same like static methods but the only difference is, the first parameter type in the extension method must be preceded by this modifier.

 

Now, we need to include Extensions namespace in our application wherever we want to access IsValidEmail extension method like as shown below.

 

using Extensions;

After adding the namespace, we can access the extension method as an instance method of extended type like as shown below.

 

string mail = "support@tutlane.com";

var result = mail.IsValidEmail();

In c#, the extension methods will be defined as static methods of static class but those are accessible as an instance method of extended type.

C# Extension Method Example

Following is the example of accessing IsValidEmail extension method by including Extensions namespace.

 

using System;

using Extensions;

 

namespace TutlaneExamples

{

    class Program

    {

        static void Main(string[] args)

        {

            string mail = "support@tutlane.com";

            var result = mail.IsValidEmail();

            Console.Write("Is valid: {0} ", result);

            Console.ReadLine();

        }

    }

}

If you observe the above example, we included Extensions namespace in our program and we are able to access IsValidEmail extension method as an instance method of string extension type.

 

When we execute the above program, we will get the result like as shown below.

 

Is valid: True

In the above example, we extended the behaviour of .NET framework System.string class type. The same way we can extend the behaviour of custom classes also based on our requirements.

 

While creating an extension method for a specific type, we need to make sure that the same signature of method will not exist for the specified type otherwise the extension method will never be called.

 

This is how we can extend the behaviour of existing types by adding new methods without modifying the original types.

C# Extension Methods Overview

Following are the important points which we need to remember about extension methods in c#.

 

  • Extension methods are useful to extend the behaviour of existing types without modifying, deriving or recompiling the original types.
  • We can create an extension method for any .NET type such as custom class, .NET framework class or third party classes.
  • In c#, the extension methods will be defined as static methods of a static class with required parameters.
  • The first parameter of the extension method will specify the type on which the method will operate on and it must be preceded by this modifier.
  • The extension methods can be used anywhere in our application by including the namespace of our extension method.
  • In c#, the extension methods can be accessed like an instance method of extended type.

Topics Covered