Header add


In this article we will discuss the LINQ Extension Methods in C#. Please follow our previous article before proceeding to this article where we discussed the Differences between IEnumerable and IQueryable in C#.

>> What is Extension Methods in C# ?
>> How to implement extension methods in C# ?
>> When to use Extension Methods in C# ?
What is Extension Methods?

An extension method is a static method of a static class that can be invoked using the instance method syntax. Extension methods are used to add new behaviors to an existing type without altering. In extension method "this" keyword is used with the first parameter and the type of the first parameter will be the type that is extended by extension method.

In simple words, we can say that the Extension methods can be used as an approach of 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.

Conceptually, extension methods provides as an implementation of the decorator structural pattern. At compile time an extension method call is translated into an ordinary static method call.

Feature and Property of Extension Methods:

Basic features and properties of extension methods are:
  • It is a static method.
  • It uses the "this" keyword as the first parameter with a type in .NET and this method will be called by a given type instance on the client side.
  • It also shown by VS intellisense. When we press the dot (.) after a type instance, then it comes in VS intellisense.
  • An extension method should be in the same namespace as it is used or you need to import the namespace of the class by a using statement.
  • You can give any name for the class that has an extension method but the class should be static.
  • If you want to add new methods to a type and you don't have the source code for it, then the solution is to use and implement extension methods of that type.
  • If you create extension methods that have the same signature methods as the type you are extending, then the extension methods will never be called.
How to implement extension methods in C#

Let's take an example to find the number of words and char without space of a sentence using LINQ Extension method.


When we run the application the expected output like below;

According to the sentence the WordCount() and charWithoutSpace() count is show

Code Explanation:
  • we create a class named as ExtensionHelper as a static class.
  • The type the method extends (i.e. string) should be passed as the first parameter preceding with the “this” keyword to the GetWordCount() method.
  • Make another method on ExtensionHelper class TotalCharWithoutSpace()
When to use Extension Methods in C#?
  • You need a method of an existing type and not the owner of the source code of that type.
  • You need a method of an existing type, you do own the source code of that type but that type is an interface.
  • 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.
Key points about extension methods
  • An extension method is defined as static method but it is called like as instance method.
  • An extension method first parameter specifies the type of the extended object, and it is preceded by the "this" keyword.
  • An extension method having the same name and signature like as an instance method will never be called since it has low priority than instance method.
  • An extension method couldn't override the existing instance methods.
  • An extension method cannot be used with fields, properties or events.
  • The compiler doesn't cause an error if two extension methods with same name and signature are defined in two different namespaces and these namespaces are included in same class file using directives. Compiler will cause an error if you will try to call one of them extension method.
In next article we will discuss LINQ Select Operator in C#


</> Find the Source Code in Github.com/CoreProgramm/

    Summary
In this tutorial we discussed LINQ Extension Methods in C#. If have any question related to this topic then give your feedback.

Post a Comment

Previous Post Next Post