Custom Extension Method in C#

Kailash Chandra Behera | Sunday, September 18, 2016

Introduction

In my previous article we discussed about extension method, this article demonstrates how to create your own extension methods for any type in the .NET Framework Class Library, or any other .NET type that you want to extend.

Getting Started

Client code can use your extension methods by adding a reference to the DLL that contains them and adding a using directive that specifies the namespace in which the extension methods are defined.

In the extension method, the first parameter specifies which type the method operates on, and the parameter is preceded by this modifier. Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.

Generally, the extension methods are defined inside a non-nested, non-generic static class, hence while creating your own extension method you should keep in mind that you are not defining extension method inside a nested, generic static class.

Above are the general guidelines for extension methods, now we will discuss the steps to create a custom extension method. First create a static class for containing extension method, make sure that class must be visible to client code which means class should not be private.

Inside the class create a static method with the same Access Modifiers. The first parameter of the method specifies the type that the method operates on, it must be preceded with the this modifier.

Now we will discuss, how to use our own extension method. Add a using directive to specify the namespace that contains the extension method class and Call your static methods like instance methods on the type.

Keep in mind that the first parameter is not specified by calling code because it represents the type on which the operator is being applied, and the compiler already knows the type of your object. You only have to provide arguments for parameters 2 through n.


Example

The below example creates an extension method named CountWords in the classStringExtension of namespace CustomExtensionMethod. The method operates on the String class, which is specified as the first method parameter. The CustomExtensionMethod namespace is imported into the application namespace, and the method is called inside the Main method.

 using System.Linq;  
 using System.Text;  
 using System;  
 namespace CustomExtensionMethod  
 {  
   //Extension methods must be defined in a static class  
   public static class StringExtension  
   {  
     // This is the extension method.  
     // The first parameter takes the "this" modifier  
     // and specifies the type for which the method is defined.  
     public static int WordCount(this String str)  
     {  
       return str.Split(new char[] {' ', '.','?'}, StringSplitOptions.RemoveEmptyEntries).Length;  
     }  
   }  
 }  

Summary

In the above of this article we discussed how to create custom extension method in c#, hope this article may helpful to you.

Thanks