find and solve || findandsolve.com
Please wait.....

How to implement and use Extension Methods in C#

In the c#, extension methods are static methods that enable you to "add" methods to existing types without creating a new derived type, recompiling, or modifying the original type such as class, struct, or interface.


In the c# programming language, you can implement our extension methods for any .NET type such as custom class, .NET framework class, or third-party classes.


To create an extension method, you 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, and it must be preceded by this modifier.


The following example of implementing an extension method for System.string class.

using System.Text.RegularExpressions;
namespace FindAndSolve
{
   public static class ExtensionExample
   {
     // Extension Method for string type
     public static bool IsEmailValid(this string objStr)
     {
        var result = Regex.IsMatch(objStr, @"^[\w-\.][email protected]([\w-]+\.)+[\w-]{2,4}$");
        return result;
     }
   }
}


If we observe given above example, we are extending the behavior of string type by creating the IsEmailValid extension method.

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


You need to include the Extensions namespace in your application wherever you want to access the IsEmailValid extension method as shown below.

using Extensions;


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


string chehckValid= "[email protected]";

var result = chehckValid.IsEmailValid();


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


C# Extension Method Example


In the following the example of accessing the IsEmailValid extension method by including the Extensions namespace.

using System;
using Extensions;//this is another class function
namespace FindAndSolve
{
    public class Program
    {
       static void Main(string[] args)
       {
          string chehckValid = "[email protected]";
          var result = chehckValid.IsValidEmail();
          Console.Write("Is valid: {0} ", result);
          Console.ReadLine();
       }
    }
}


Output

Is valid: True


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


In the given above example, extending the behavior of the .NET framework System.string class type. In the same way, you can extend the behavior of custom classes also based on our requirements.


While creating an extension method for a specific type, you need to ensure that the same signature method will not exist for the specified type; otherwise, the extension method will never be called.


C# Extension Methods Overview


  • In c#, Extension methods are useful to extend the behavior of existing types without modifying, deriving, or recompiling the original types.
  • You can create an extension method for any .NET types like a custom class, .NET framework class, third party classes, etc.
  • 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, and it must be preceded by this modifier.
  • The extension methods can be used anywhere in your application by including the namespace of our extension method.
  • In c# programming language, the extension methods can access such as for instance method of extended type.