Introduction.
Hello Guys,
In this article, I will explain what is extension method and why and where we should use this.
Let’s understand the term extension means we are going to create something new with the help of old made things or objects etc. right? So this is very predictable word in real world. On other hand in programming world this meaning is almost same with some new enhancements.
Let’s see the professional definition in terms of oops.
Extension method is special kind of static method which we can access throughout or outside of program.
Extension method help us to extend the class with new methods without any changes in base type or class .
We can create new methods in the dll library class also without changing the old code though extension method.
Let’s see the simple example of extension method using System;
namespace csharpdemoexample
{
public static class csharpdemo
{
public static void Main()
{
try
{
Console.WriteLine(Myextmethod(“Welcome”, “Extension”));
Console.Read();
}
catch (Exception ex)
{
throw ex;
}
}
public static string Myextmethod(this string a, string b)
{
return (a + ” ” + b); // This is the extension method named Myextmethod.
}
}
}
All right. Now one question would come to your mind is, why I have written the this keyword in parameter right?
So let me clarify this confusion. This is the keyword which require for first argument parameter no matter what data type you use for this. But this keyword is mandatory for extension method. If you are not defining the this, it will acts like a normal static method .
Now next question is how to use the extension method in dll file. In other words, if you have created the dll in C# so all the classes and method will be act as private. So how can we use the same dll and create the new method inside your new application.
So let’s see how we can achieve this task? Let’s add a Class library in your solution project.
Created two methods inside dll class. Add and subtract right .
Now I want to use this DLL into some project and using this class, I want to create one new method.
Like Multiply. So we will achieve this by extension method. Let’s see how? First, add the DLL reference inside you project in which you want to implement the new method. Okay?
using BaseDLL;
namespace csharpdemoexample
{
public static class csharpdemo
{
public static int Multiply(this MathDemo obj ,int a, int b)
{
return a * b; //This is the extension method i have created using DLL.
}
public static void Main()
{
try
{
MathDemo obj2 = new MathDemo();
Console.WriteLine(“Result of extension method multiply is :” + Multiply(obj2,10,20));
Console.Read(); //I have called the newly created method here. And it works like charm. Great isn't it ?
}
catch (Exception ex)
{
throw ex;
}
}
}
}
Conclusion – Now I am done. So in this article we have seen what is extension method and how to create and use them.
Thanks for reading this article.
I hope this article will be helpful for you guys, your comments and feedback are always welcome.
Thanks,
Surya Prakash Pandey.