Introduction
To put it in a simple manner, Extension Methods have been introduced in the .NET 3.5 framework to add methods to a class without altering the code of that class. Most of the time, you don't have access to create your own methods in the third-party DLLs, and you want to write your own method in that class, then the first question that comes to your mind is, how will I handle it?
The answer is simple Extension Methods.
Why do we need an Extension Method?
Another question is, why do we need to use the extension methods? We can create our own methods in the client application or in other class libraries, and we can call those methods from the client application, but the important point is that these methods would not be part of that class (class of the third party dll). So adding the extension methods in the class, you are indirectly including another behavior in that class. I am talking about the class, which is part of the third-party dll (Most Important). So when you create your own extension methods for specific types, those methods would be accessible for that type.
This can be easily understood by the following diagram.
(I am the object of the class)
After some time, the client application wants to add another behavior in that class; let's say 'talk', then extension methods can be used to add this extra behavior in that class without altering any code.
ImplementationExtension Methods in .NET
The following is the structure of the third-party class, which is not accessible to the developer. This class contains a method called 'Walk'.
public class Class1 {
public String walk() {
return "I can walk";
}
}
Now it's time to create the Extension Method. Just use the following steps.
Step 1. Create the Static Class
Step 2. Create the Static Method and give any logical name to it.
Step 3. In the Extension Method Parameter, just pass this along with the object type, and
The object name is given below.
The client application can access both the method "Walk" and the extension method "TalkExtMethod".
Benefits Extension Methods in .NET
There may be various benefits of using the Extension Methods in your code. Let's say you are using the method1 () of the class in the third-party dll, and the object instantiation has failed due to another reason; then you will get the object reference error while calling the method1 () if you did not handle the null reference check.
Class1 obj = getClass1Object();
// If the obj is null, you will get the object reference error
obj.method1();
Although the following code is safer than the previous one, the problem is you will have to include this check every time you call Method1, and if the developer forgets to include this check in the code, then the program throws an object reference error.
Class1 obj = getClass1Object();
if (obj != null) {
obj.method1();
}
If we use the extension method to solve this problem, we get outstanding results. Instead of checking the null reference every time in the code, we handle it in the extension methods, which are called by the client application.
public static string Method1ExtMethod(this Class1 objClass)
{
// If the obj is not null, then call the Method1()
if (objClass != null)
{
return objClass.Method1();
}
return string.Emp ty;
}
Class1 obj = GetClass1Object();
// Don't include any check whether the object is null or not
obj.Method1ExtMethod();