Extension Method in C#

This is the third article of the LINQ series. In this article, we will learn what extension methods are and how to create and implement your own extension method.

Extension methods are a special type of static methods that are called as if they are an instance method of the extended type.

LINQ's statement query operators like select are implemented in the Enumerable class as extension methods on the IEnumerable<T> interface.

code

Look at the preceding demo, in spite of the Where() method not belonging to the Dictionary<key, value> class, we are still able to use it as if it belongs to the Dictionary class. This is possible because the Where() method is implemented as an extension method in the IEnumerable<T> interface and the Dictionary class implements the IEnumerable<T> interface.

Demo

Now let's see how to create an extension method.

In our project, we want to create a method that process a name and the first letter of the name should not be capitalized, if it is in upper case then it will change the letter into lower case.

We need to create a helper class in which we will create our extension method.

  1. class MyExtension  
  2. {  
  3.     public static string ChangeCase(string Input) {  
  4.   
  5.         //The first step is to check if the input length is greater than zero.  
  6.         if (Input.Length > 0) {  
  7.   
  8.             //If the input is greater than zero then the next step is to convert the input value to charArray and the return type of charArray is char[].  
  9.             char[] c = Input.ToCharArray();  
  10.   
  11.             //the third step is to check if the input’s first character is in upper case or not and for that we can use IsUpper() method which returns true or false and to get to the first character we can pass index value 0.  
  12.             if (char.IsUpper(c[0])) {  
  13.   
  14.                 //if the input’s first character is already an upper case letter then convert the uppercase letter to the lowercase by invoking the ToLower() method of the char struct and inside this method pass the index value of c. The return type of ToLower() method is char and we are assigning the value back to the char c’s first index which holds the first letter of the input value.  
  15.                 c[0] = char.ToLower(c[0]);  
  16.             }  
  17.   
  18.             else{  
  19.   
  20.                 //if the case is not in uppercase then convert it into uppercase  
  21.                 c[0] = char.ToUpper(c[0]);  
  22.             }  
  23.   
  24.             //after the conversion return a new string back  
  25.             return new string(c);  
  26.         }  
  27.           
  28.         else {  
  29.                 return Input;  
  30.         }  
  31.    }  

So, now we can call this method in our main method.

main method

Run the application

Run the application

We have the output we wanted. But we haven't done it the way we wanted to, using that ChangeCase method.

We don't want MyExtension.ChangeCase(Name);

We want string result = name.ChangeCase();. We want to invoke this ChangeCase() method as if it is an instance method defined within the string class.

Currently our ChangeCase() method is not an extension method and to convert this method into an extension method, we need to make some changes.

The class should be static, so what do we need to extend?

We need to extend our string class, so the string parameter should be the first parameter within this function and the other thing we need to do is to precede the type name (string) with this keyword that we are extending.

extending

Note

We can add more than one parameter but the first parameter should always be the one we want to extend.

Now let's see if, after making these changes, our application works the way we expected.

changes our application

Look at the image above, now we are able to use the ChangeCase() method as an extension method and look at the type of this method, it states extension and that proves it.

Now we don't even need to pass any parameter in this method.

parameter

Run the application

Run

You might be thinking that without passing a parameter, how is the first letter converted into lower case?

When we invoke the ChangeCase() extension method and run the application, behind the scenes this extension method is converted into:

  1. string ChangedCase = MyExtension.ChangeCase(name); 

The extension method is just a syntactic sugar that makes the developer's life sweeter.

In the next article we will see various Aggregate functions and the use of these functions.

Until then keep learning.

Thank you.


Recommended Free Ebook
Similar Articles