Updated on 06 Oct 2025 by Admin

Extension Methods

Extension methods enable us to "add" methods to existing types so that they can be called as an instance method. This means that there's no difference between calling an extension method and the methods defined within the original type. In short, we can say that the extension methods help us to add methods to an existing class without creating a new derived type, recompiling, or otherwise modifying it. Extension methods are static methods, but they're called as if they were instance or original methods on the extended type. For client code written in C#, F#, and Visual Basic, there is no difference between an extension method and an instance method.

Let's understand the extension method with an example. Think about the String class, which is defined in the .NET Framework under the System namespace. The String class contains various methods like Compare(), Concat(), Replace(), ToUpper(), ToLower() and a lots of other helpful methods.

But for some reason, suppose you want to add some more methods to the String class. Here things become complicated, as the String class is a .NET-defined class, so you can't touch it. It's not as easy as opening a class file and starting to add a new method to it. The next option is to create a base class with the method you want and inherit the String class from your base class, but it is also not possible, because the String class is not user-defined. The third option is to create a wrapper class as shown below:

public  class StringHelper
{
public static string RemoveVowels(string str)
{
char[] vowels = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
List<char> al = vowels.OfType<char> ().ToList(); 
StringBuilder sb = new StringBuilder(str);
for (int i = 0; i < sb.Length; i++)
{
if (al.Contains(sb[i]))
{
sb.Replace(sb[i].ToString(), "");
i--;
}
}
return sb.ToString(); 
}
}
public class Program    
{       
public static void Main()    
{
string str = @"My name is Sachin Kumar Singh";
Console.WriteLine("With vowels---"+ str);
Console.WriteLine("without vowels----"+StringHelper.RemoveVowels(str));
Console.ReadLine();
}
}

Extension method
Wrapper class's method

This method works perfectly, but still we are not able to call the method directly from a string instance, because the RemoveVowels() method is not an instance (original) method of the String class, so we can't do something as shown below:

string str="My name is Sachin";
str.RemoveVowels() //  not possible.

This is where we need to create an extension method. To create an extension method, you just need to follow three steps:

  • Step 1. Mark the class as static.
  • Step 2. Mark the method as static.
  • Step 3. Make the type (in our case, string) that you want to extend the first parameter of the method, and add this keyword before it.

public static class StringHelper //First Step: Making the class static
{
//Second Step: Making the method static
public static string RemoveVowels(this string str) //Third Step: Use 'this' keyword with the type you are extending
{
char[] vowels = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
List<char> al = vowels.OfType<char>().ToList(); 
StringBuilder sb = new StringBuilder(str);
for (int i = 0; i < sb.Length; i++)
{
if (al.Contains(sb[i]))
{
sb.Replace(sb[i].ToString(), "");
i--;
}
}
return sb.ToString(); 
}
}

And, now we can use the method as an instance method of the string class, as shown below:

public class Program
{       
public static void Main()    
{
string str = @"My name is Sachin Kumar Singh";
string withoutVowel = str.RemoveVowels(); //Notice here 
Console.WriteLine(withoutVowel);
Console.ReadLine();
}
}

You must be wondering why we are discussing extension methods in a LINQ tutorial. It is because almost all LINQ standard query operators are extension methods that add query functionality to the existing System.Collections.IEnumerable and System.Collections.Generic.IEnumerable<T> types. LINQ operators are extension methods on the IEnumerable type. For example, the Generic List< T> class doesn't have the Where() method, but still we can use Where() method as an instance method with a List type, as shown below:

List<int> nums= new List<int>{1,2,3,4,5,6,7,8,9,10};
var evenNums= nums.Where(x=>x%2==0);

We are able to use Where() method on List<T> because the generic List<T> class implements the IEnumerable and Where() method is an extension method on IEnumerable type.


You need to login to post a comment.

Sharpen Your Skills with These Next Guides