In this article, we are going to discuss the LINQ Extension Methods in C# with Examples. 

What are Linq Extension Methods in C#?

According to MSDN, Extension methods allow us to add methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type

In simple words, we can say that the Linq Extension methods can be used as an approach to extending the functionality of a class by adding new methods in the future if the source code of the class is not available or if we don’t have any permission in making changes to the class.

The most important point that you need to keep in mind is, extension methods are the special kind of static methods of a static class, but they are going to be called as if they were instance methods on the extended type. 

When to use Linq Extension Methods in C#?

You need to use an extension method if any of the following conditions are true:

  1. You need a method on an existing type and you are not the owner of the source code of that type.
  2. You need a method on an existing type, you do own the source code of that type but that type is an interface.
  3. You need a method on an existing type, you do own the source code and that type is not an interface but adding the method creates undesired coupling.

Otherwise, you should go with the normal method of the actual type itself.

How to Implement Linq Extension Methods in C#?

Let us understand this with an example. Our requirement is that we want to add a method in the built-in string class, let’s call this method GetWordCount() which will count the word present in a string separated by a space.

For example, if the string is “Welcome to Dotnet Tutorials”, then it should return the word count as 4. The most important point is that we need to call this method on the String object as shown below.
int wordCount = sentence.GetWordCount();

Note: We cannot define the GetWordCount() method directly in the string class as we are not the owner of the string class. The string class belongs to the System namespace which is owned by the .NET framework. So, the alternative solution to achieve this is to write a wrapper class as shown below.

The above ExtensionHelper Wrapper class works fine, but the problem is, here we cannot call the GetWordCount() method using the string object as shown below.
int wordCount = sentence.GetWordCount();

Instead, we need to call the GetWordCount() method as shown below.
int wordCount = ExtensionHelper.GetWordCount(sentence);

How to convert the above GetWordCount() method to an Extension Method of the String class?

Now let’s convert the GetWordCount() method to an extension method on the String class. So that we can able to call the GetWordCount() method using the following syntax.
int wordCount = sentence.GetWordCount();

In order to make the above GetWordCount() method an extension method, we need to make two changes which are as follows.

  1. First, we need to make the ExtensionHelper class a static class.
  2. Second, the type the method extends (i.e. string) should be passed as the first parameter preceding the “this” keyword to the GetWordCount() method.

With the above two changes in place, now the GetWordCount() method becomes an extension method and hence we can call the GetWordCount() method in the same way as we call an instance method of a class. The complete example code is given below.

Back to Course Content

Leave a Comment