Extension methods in C# are used to add methods to an existing type without modifying the type.If there is some existing type then for adding some new functionality to the type we can either modify the type or derive a new type that extends the original type.Both of these approaches requires recompiling the source code.Since the source code is required to be modified hence this approach suffers from the following disadvantages
- Many times we don’t have access to the source code such as while using the code written by other developers.
- Modifying the original type requires recompilation and retesting of the existing functionality
Suppose we have a class called Calculate which defines a method called Sum.
class Calculate { public int Sum(int a,int b) { return a + b; } }
Now we have a requirement to add Substract method to this class.We don’t have access to the source code of this class as this was developed by another developer.So we can not modify this class to add the Substract method.This is where we can use extension method to add the Substract method to the Calculate class.
Defining extension methods is easy ,we just need to do the following for defining an extension method:
- We have to define our method as static in a static class.
- The first parameter to the extension method should be declared with the “this” keyword.
So we can add Substract method to the Calculate class as:
static class CalculateExtension { public static int Substract(this Calculate cal, int a, int b) { return a - b; } }
We can consume the Substract extension method just like the Calculate method.So for the client of the Calculate class there is no difference between the method defined in the class and the extension method:
static void Main(string[] args) { Calculate cal=new Calculate(); int total=cal.Sum(1,8); Console.WriteLine("Total={0}",total); int difference=cal.Substract(5,2); Console.WriteLine("Difference={0}",difference); Console.ReadLine(); }
To use the extension method defined on a type we should import the namespace in which the class containing the extension method is defined.Mostly the class in which we define the extension method will be in a different namespace then the class for which the extension method is defined.
For example the Calculate class and the CalculateExtension class in the above example could be defined in a separate namespaces.So we can define the CalculateExtension class in a namespace called Extension
namespace Extension { static class CalculateExtension { public static int Substract(this Calculate cal, int a, int b) { return a - b; } } }
Now to use the above extension method we just need to import the Extension namespace.Once we import the Extension namespace through the using Extension; import statement we can call the Substract method using an object of the Calculate class as if it is defined by the Calculate class.
A common example where extension methods are used in c# are the LINQ query operators.These query operators are defined in the System.Linq namespace and extend the collection classes.To use these query operators we need to bring the System.Linq namespace in scope:
using System.Linq;
To see these query operators in action we can use a normal array.We create an array object and see the methods defined by the array object we can see the following methods
But now if we bring the query operators in scope by using the using System.Linq; statement we are able to see the following methods on the same array object
The extra methods which we are able to see are the extension methods provided by the classes in the System.Linq namespace.
Leave a Reply