Understanding Delegates:
Hi Readers,
Today I want to take a very confusing topic.(At least was for me, when I first went through it).
It is bit Confusing, as many of us actually don't know what is the use of Delegates or in what scenarios it should be used.
Now if you search about Delegates, you would get something like,"Delegates are function Pointers!"
So what actually is a function pointer?
As the name suggests it may be a reference to a function!
True indeed,but why shall we use it?
For the Readers for whom concept of delegate is completely new, I request them to read from "Delegate Explanation Start" to "Delegate Explanation End " first ,others can Skip this block.
Delegate Explanation Start:
Prerequisite: Some knowledge about OOPS. :)
Well a delegate is a type safe function pointer. i.e it holds a reference (pointer) to a function.
Wondering what is type safe!?
The signature of the delegate must match the signature of the function, the delegate points to,otherwise you will get a compilier eror.
This is the reason delegates are called type safe function pointers.
Delegates are of Reference type,so you need to create an instance of it and pass a function as the parameter to its constructor.
This is how we declare a delegate:
Access Specifier delegate ReturnType Name(Method Reference).
Example:
public delegate void MyFirstDelegate(string Message);
class Test
{
public static void Main()
{
MyFirstDelegate myDel = new MyFirstDelegate(Yo);
myDel ("Delegates are awesome");
}
public static void Yo(string strMessge)
{
Console.WriteLine(strMessge);
}
}
If you run the above code, as you may expect you would get result as "Delegates are awesome".
This is the basics of how delegates work.
But did you understand why shall we use delegates ? It looks idiotic to create a method(here "Yo") then create instance of a delegate(MyFirstDelegate) and then passing the method(Yo), Isn't it??
You may argue that we could have directly created an instance of the "Test" class and call the method! then why the heck we have to use delegate?? Just a stupid concept!!??
Not actually!
Please Read the below Explanation. "Real Use of Delegates":) ;)
Delegate Explanation End.
Real Use of Delegates:
Now lets Assume that you are a Project head of a Products Company(Sounds good right! ;)).
And you are responsible to create a product which returns the Employees' name of different organisation who are eligible for promotion.
Obviously different organisations may have different criteria to promote its Employees,in that case we cant hard code the logic in our base product, as we have to sell it to different organisation and hard coding the logic for one organisation would mean creating 'n' number of products for 'n' number of organisations!! Which we obviously don't wanna do.
Then want can be done in this scenario!!?? Any idea?
Surprisingly,Delegates are the go to Concept!
how?
Lets see ;):
#region GenericLogic //think of this as a Product
#region--a generic function pointer
public delegate Boolean Promotable(Employee emp);
#endregion
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public int Experience { get; set; }
public void PromoteEmployee(List<Employee> empList,Promotable obj)
//need the list of Employees and the logic
{
foreach(Employee emp in empList)
{
if (obj(emp))
{
Console.WriteLine("{0} is Promoted",emp.Name);
}
}
}
}
#endregion
Think of region "GenericLogic" as our Product, which we will be selling.
Here you can see that our Product just needs the list of employees of the organisation and the logic for promoting those employees.
So now we would ask our client to just give there employee list and the logic specific to there organisation so that they would get the Employee Names who are eligible foe promotion.
What client will do? :
They would just call the Delegate and pass there own logic!!
public class ClientClass
{
public static void Main(string[] args)
{
#region company has to call the product's method to get the list of Employees Eligible for promotion by giving its internal logic and its employees
#region company forming an employee list //this is Private data of company!!
List<Employee> empList = new List<Employee>();
empList.Add(new Employee()
{
ID = 1,
Name = "asd",
Experience = 4,
Salary = 123
}
);
empList.Add(new Employee()
{
ID = 1,
Name = "ashish",
Experience = 7,
Salary = 123567
});
empList.Add(new Employee()
{
ID = 1,
Name = "asddfg",
Experience = 14,
Salary = 12453
});
empList.Add(new Employee()
{
ID = 1,
Name = "asdff",
Experience = 3,
Salary = 23
});
#endregion
#region now the company has to have a method(logic) for the promotion of its employees,
Promotable obj3 = new Promotable(Test.IsPromotable); //making use of Product's Delegate,pls find the logic of IsPromotable
#endregion
#region now passing the values to the shared function to the product
Employee obj = new Employee();
obj.PromoteEmployee(empList, obj3);
#endregion
#endregion
}
#region IsPromotable Method--logic of a particular company to promote its employees
public static Boolean IsPromotable(Employee Emp)
{
if (Emp.Experience >= 5)
{
return true;
}
else
return false;
}
#endregion
}
#endregion
Now each and Every client may have different logic to promote there Employees!.
so ,they can just provide there logic to the base product and get the output!.
I hope it is clear.
Please Leave your comments ! :)
Thank you.
Regards,
Ashish Agarwal
Hi Readers,
Today I want to take a very confusing topic.(At least was for me, when I first went through it).
It is bit Confusing, as many of us actually don't know what is the use of Delegates or in what scenarios it should be used.
Now if you search about Delegates, you would get something like,"Delegates are function Pointers!"
So what actually is a function pointer?
As the name suggests it may be a reference to a function!
True indeed,but why shall we use it?
For the Readers for whom concept of delegate is completely new, I request them to read from "Delegate Explanation Start" to "Delegate Explanation End " first ,others can Skip this block.
Delegate Explanation Start:
Prerequisite: Some knowledge about OOPS. :)
Well a delegate is a type safe function pointer. i.e it holds a reference (pointer) to a function.
Wondering what is type safe!?
The signature of the delegate must match the signature of the function, the delegate points to,otherwise you will get a compilier eror.
This is the reason delegates are called type safe function pointers.
Delegates are of Reference type,so you need to create an instance of it and pass a function as the parameter to its constructor.
This is how we declare a delegate:
Access Specifier delegate ReturnType Name(Method Reference).
Example:
public delegate void MyFirstDelegate(string Message);
class Test
{
public static void Main()
{
MyFirstDelegate myDel = new MyFirstDelegate(Yo);
myDel ("Delegates are awesome");
}
public static void Yo(string strMessge)
{
Console.WriteLine(strMessge);
}
}
If you run the above code, as you may expect you would get result as "Delegates are awesome".
This is the basics of how delegates work.
But did you understand why shall we use delegates ? It looks idiotic to create a method(here "Yo") then create instance of a delegate(MyFirstDelegate) and then passing the method(Yo), Isn't it??
You may argue that we could have directly created an instance of the "Test" class and call the method! then why the heck we have to use delegate?? Just a stupid concept!!??
Not actually!
Please Read the below Explanation. "Real Use of Delegates":) ;)
Delegate Explanation End.
Real Use of Delegates:
Now lets Assume that you are a Project head of a Products Company(Sounds good right! ;)).
And you are responsible to create a product which returns the Employees' name of different organisation who are eligible for promotion.
Obviously different organisations may have different criteria to promote its Employees,in that case we cant hard code the logic in our base product, as we have to sell it to different organisation and hard coding the logic for one organisation would mean creating 'n' number of products for 'n' number of organisations!! Which we obviously don't wanna do.
Then want can be done in this scenario!!?? Any idea?
Surprisingly,Delegates are the go to Concept!
how?
Lets see ;):
#region GenericLogic //think of this as a Product
#region--a generic function pointer
public delegate Boolean Promotable(Employee emp);
#endregion
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public int Experience { get; set; }
public void PromoteEmployee(List<Employee> empList,Promotable obj)
//need the list of Employees and the logic
{
foreach(Employee emp in empList)
{
if (obj(emp))
{
Console.WriteLine("{0} is Promoted",emp.Name);
}
}
}
}
#endregion
Think of region "GenericLogic" as our Product, which we will be selling.
Here you can see that our Product just needs the list of employees of the organisation and the logic for promoting those employees.
So now we would ask our client to just give there employee list and the logic specific to there organisation so that they would get the Employee Names who are eligible foe promotion.
What client will do? :
They would just call the Delegate and pass there own logic!!
public class ClientClass
{
public static void Main(string[] args)
{
#region company has to call the product's method to get the list of Employees Eligible for promotion by giving its internal logic and its employees
#region company forming an employee list //this is Private data of company!!
List<Employee> empList = new List<Employee>();
empList.Add(new Employee()
{
ID = 1,
Name = "asd",
Experience = 4,
Salary = 123
}
);
empList.Add(new Employee()
{
ID = 1,
Name = "ashish",
Experience = 7,
Salary = 123567
});
empList.Add(new Employee()
{
ID = 1,
Name = "asddfg",
Experience = 14,
Salary = 12453
});
empList.Add(new Employee()
{
ID = 1,
Name = "asdff",
Experience = 3,
Salary = 23
});
#endregion
#region now the company has to have a method(logic) for the promotion of its employees,
Promotable obj3 = new Promotable(Test.IsPromotable); //making use of Product's Delegate,pls find the logic of IsPromotable
#endregion
#region now passing the values to the shared function to the product
Employee obj = new Employee();
obj.PromoteEmployee(empList, obj3);
#endregion
#endregion
}
#region IsPromotable Method--logic of a particular company to promote its employees
public static Boolean IsPromotable(Employee Emp)
{
if (Emp.Experience >= 5)
{
return true;
}
else
return false;
}
#endregion
}
#endregion
Now each and Every client may have different logic to promote there Employees!.
so ,they can just provide there logic to the base product and get the output!.
I hope it is clear.
Please Leave your comments ! :)
Thank you.
Regards,
Ashish Agarwal
No comments:
Post a Comment