Introduction
In this article we will learn what are extension method? Why extension method used in c#. Why do we need extension methods in c#? Extension methods with example.
Previous Updates
In previous articles we have learnt what is TypeScript and How to Install this on VS2015 .AngularJS basics, Difference IEnumerabe vs IQueryable. What is Cursor in sql and use of cursor. Best basic C# interview questions.
What Is Extension Method
Extension Method allows us to enable you to add methods to existing types without any creation of a new derived type and recompiling. Means in other simple words we can say by using extension method we can add functionalities in an existing class without modifying it, extending it, or re-compiling it. The Extension Method was introduced in C# Version 3.0.
Extension methods are Static method or also known as instance methods.
What happend if we dont have Extension Method
Suppose if you used a DLL file in your project /application for giving flexibility to your application then at any moment you feel that you need some more from that DLL file means if your DLL have only Add method and now you wants a Subtract method from that then you have to Add an additional subtract method on that DLL.And also update the DLL reference for this.
Sounds like a Matted solution so Lets do a example
I have an Console Application in Which i am using a DLL GetEmployee to return EmpSalary.
namespace GetEmployee
{
public class AllEmpList
{
public int GetEmployeeSalaryByName(string name)
{
int salary = 0;
//Get EmployeeSalary Code here
return salary;
}
}
}
|
I had successfully added this DLL reference in my Project .
And below is my application code where i use this class for getting EmpDetail.
Lets assume what happend if i want to return Employee Salary based on EmployeeID. Then for this kind of situations i will go back on my DLL and add GetSalaryByEmpID method and also update the DLL reference.
But what happend if i don't have code access of that DLL? so how can we achieve this. Without changing in that Type or class? how can i add a method on that class which is directly access through object of that class.
Now here we can understand the use and Concept of Extension Method in live scenario.
Create Extension Method
We can create Extension Method in our application in few simple steps.
1. Define a static class which will contain the Extension Method or methods.
2. Create a static method in that Class with Same AccessLevel of class type.
3. Use a DLL name on class namespace .
4. The first parameter of the Extension Method always specifies the type that the method operates on. Make sure the type name is preceded with the "this" modifier.
Now as we again talking about above given example if i want to add a method GetSalaryByEmpID which will access through the DLL object. For this i add a NewClass with in my ConsoleApplication and named it WrapperEmployee. And on this i create a Extension method.
namespace ConsoleApplication1
{
static class WrapperEmployee
{
public static int GetsalaryByEmpID(this AllEmpList empDetail,
int empID)
{
int salary = 0;
//Get EmployeeSalary Code here
return salary;
}
}
} |
Here in this code you can see all the steps which we learn above how to create extension method. first i create a Static class then a static method / extension method and pass the dll class as a parameter with this keyword.
Now go back to the main class and use this ExtensionMethod by using DLL object.
We done it. You can see in the above image GetSalaryByEmpID method is displaying with in the AllEmpList DLL class's object reference. And if you see the highlighted image you remember the icon of Extension Method. So without changing on to that DLL we successfully add the method and use the same with DLL reference.
0 comments:
Post a Comment