1. Definition:
A Delegate is a type safe function pointers and it hold reference(i.e. Pointer) to a function.
- Delegates are like reference types like classes and interfaces. Unlike structures are value type.
- The reasont o say delegate as type safe because delegate must match the signature of the function, the delegate points to, otherwise you get a compiler error.
Syntax: Same a member function with a keyword delegate
public delegate void func(string param);
class demo
{
public static void Main()
{
// ignature must match the signature of the delegate
func del = new func(method);
// Invoke the delegate, which will invoke the method
del("Hello from Delegte");
}
public static void method(string param)
{
Console.WriteLine(param);
}
}
Note:
Compare parameter of the method and delegate parameters and return type. If the parameters don't match then it would lead to the compiler error.
2. Why Delegates:
Here in below example, the employee is promoted if the salary is greater than 5000, let say if some other organization wants to have a different rule for promotion. In such case reusable, since we have hard coded value, hence it is not flexible.
Example:
class demo
{
public static void main()
{
List
list_org.Add(new organization() { empId = 101, Name = "iliyas", salary = 5000 });
list_org.Add(new organization() { empId = 101, Name = "patel", salary = 2000 });
list_org.Add(new organization() { empId = 101, Name = "kabir", salary = 6000 });
organization.promoteemp(list_org);
}
}
class organization
{
public int empId { get; set; }
public string Name { get; set; }
public int salary { get; set; }
public static bool promoteemp(List
{
foreach (organization orgn in list_org)
{
if (orgn.salary >= 5000)
{
Console.WriteLine("Promoted : " + orgn.Name);
}
}
}
}
With Delegate:
class demo
{
public static void main()
{
List
list_org.Add(new organization() { empId = 101, Name = "iliyas", salary = 5000 });
list_org.Add(new organization() { empId = 101, Name = "patel", salary = 2000 });
list_org.Add(new organization() { empId = 101, Name = "kabir", salary = 6000 });
IsPromotable isPromote = new IsPromotable(Promote);
organization.promoteemp(list_org, isPromote);
}
public bool Promote(Employee emp)
{
if(emp.salary >= 5000)
return true
else
return false;
}
}
delegate bool IsPromotable(List
class organization
{
public int empId { get; set; }
public string Name { get; set; }
public int salary { get; set; }
public static bool promoteemp(List
{
foreach (organization orgn in list_org)
{
if (IsEligible(orgn))
{
Console.WriteLine("Promoted : " + orgn.Name);
}
}
}
}
With Delegate using Lambda expression:
class demo
{
public static void main()
{
List
list_org.Add(new organization() { empId = 101, Name = "iliyas", salary = 5000 });
list_org.Add(new organization() { empId = 101, Name = "patel", salary = 2000 });
list_org.Add(new organization() { empId = 101, Name = "kabir", salary = 6000 });
organization.promoteemp(list_org, emp=>emp.salary >= 5000);
}
}
delegate bool IsPromotable(List
class organization
{
public int empId { get; set; }
public string Name { get; set; }
public int salary { get; set; }
public static bool promoteemp(List
{
foreach (organization orgn in list_org)
{
if (IsEligible(orgn))
{
Console.WriteLine("Promoted : " + orgn.Name);
}
}
}
}
Comments
Post a Comment