Introduction
ูู ุงูู
ูุงู ุฏูุ ููุชููู
ุนู ุฃู
ุซูุฉ ูุงูุนูุฉ ููู Delegates ูู C#. ุงูู delegates ูู ูุงุญุฏุฉ ู
ู ุฃูู
ุงูู
ูุงููู
ุงููู ูุงุฒู
ุชููู
ูุง ูู
ุทูุฑ C#. ูู ุงูุชุฑฺคูููุงุช ูุชูุฑุ ู
ุนุธู
ุงูุงูุชุฑฺคููุฑุฒ ุจูุทูุจูุง ู
ูู ุชุดุฑุญ ุงุณุชุฎุฏุงู
ุงูู delegates ูู ุงูู
ุดุงุฑูุน ุงููุงูุนูุฉ ุงููู ุงุดุชุบูุช ุนูููุง.
ูุง ุฑูุช ุชูุฑุง ุงูู
ูุงููู ุฏููุ ุงููู ูุงูุดูุง ูููู
ุฃุณุงุณูุงุช ุงูู delegate ูุงูู multicast delegates:
Single Cast Delegates in C#Multicast Delegate in C#
ุงูู Delegates ูู C# ุจุชูุณุชุฎุฏู
ุจุดูู ูุจูุฑ ู
ู ู
ุทูุฑู ุงูู framework. ุฎูููุง ูููู
ุงูู delegates ุจู
ุซุงููู ู
ู ุงููุงูุน.
Delegate Real-Time Example 1: Worker Notifications
ุฎูููุง ูููู ุฅู ุนูุฏูุง class ุงุณู
ู Worker ูุงูู class ุฏู ููู method ุงุณู
ูุง DoWork. ู
ุชุทูุจุงุช ุงูุจูุฒูุณ ุจุชุงุนุชูุง ูู ุฅููุง ูู
ุง ูุณุชุฏุนู DoWork methodุ ู
ุญุชุงุฌูู ูุจุนุช ุฅุดุนุงุฑ ุจูุณุจุฉ ุงูุดุบู ุงููู ุฎูุต ููู
ุณุชููู (consumer)ุ ููู
ุงู ูู
ุง ุงูุดุบู ูุฎูุตุ ู
ุญุชุงุฌูู ุจุฑุถู ูุจุนุช ุฅุดุนุงุฑ.
- ู ุซูุงู: ูุฏ ุฅูู ู ู ุงูุดุบู ุฎูุต ูู ุณุงุนุฉุ ูู ุณุงุนุชููุ ูู ุชูุงุช ุณุงุนุงุชุ ูููุฐุง ูุญุฏ ู ุง ุงูุดุบู ูุฎูุต.
- ูุงูู
DoWork methodู ุนูุฏูุงุด ุฃู ููุฑุฉ ุชุจุนุช ุงูุฅุดุนุงุฑุงุช ุฏู ูู ูู. ุงููู ุจูุณุชุฏุนูDoWork methodูู ุงููู ุงูู ูุฑูุถ ููุฑุฑ ูุจุนุช ุงูุฅุดุนุงุฑ ูู ูู. ุฏู ู ุนูุงู ุฅููุง ููุง ู ุญุชุงุฌูู ูุณุชุฎุฏูcall-back functions.
ุฏู ุณููุงุฑูู ู
ุซุงูู ู
ุญุชุงุฌูู ูุณุชุฎุฏู
ููู delegates. ููุนู
ู class ุงุณู
ู Worker.cs.
ููุงุ ุนู
ููุง ุงุชููู delegates. ุงูู DoWork method ุจุชุงุฎุฏ ุฃุฑุจุนุฉ parametersุ ุงุชููู ู
ููู
ู
ู ููุน delegate. ุจุนุฏูู ุฌูู ุงูู methodุ ุจูุณุชุฎุฏู
for loop ููู ู
ุฑุฉ ุงูู loop ุจุชุชููุฐุ ุจูุนู
ู ุดููุฉ ุดุบู ูุจูุฎุทุฑ ุงูู
ุณุชุฎุฏู
ุนู ุทุฑูู ุงุณุชุฏุนุงุก ุงูู delegate. ุจู
ุฌุฑุฏ ู
ุง ุชูููุฐ ุงูู loop ุจูุฎูุตุ ุฏู ู
ุนูุงู ุฅู ุดุบููุง ุฎูุตุ ูุจุนุฏูู ุจูุณุชุฏุนู work completed delegate ุนุดุงู ูุฎุทุฑ ุงูู
ุณุชุฎุฏู
ุฅู ุงูุดุบู ุฎูุต.
Worker.cs
using System.Threading;
namespace DelegatesRealTimeExample
{
// Delegate for reporting progress
public delegate void WorkPerformedHandler(int hours, string workType);
// Delegate for reporting completion
public delegate void WorkCompletedHandler(string workType);
public class Worker
{
public void DoWork(int hours, string workType, WorkPerformedHandler del1, WorkCompletedHandler del2)
{
// Do Work here and notify the consumer that work has been performed
for (int i = 0; i < hours; i++)
{
// Do Some Processing
Thread.Sleep(1000); // Simulate work for 1 second
// Notify how much work has been done
del1(i + 1, workType);
}
// Notify the consumer that work has been completed
del2(workType);
}
}
}ุฏูููุชูุ ุนุฏูู Program class ูุงูุชุงูู. ููุงุ ุจูุนู
ู ุงุชููู call-back methods ุจููุณ ุงูู signature ุจุชุงุน ุงูู delegates ุจุชุงุนุชูุง. ุจุนุฏูู ุจูุนู
ู instances ู
ู delegate ููู
ุงู ุจูุนู
ู instance ู
ู Worker class ูุจูุณุชุฏุนู DoWork method ุจุฅููุง ูู
ุฑุฑ ุงูููู
ุงูู
ุทููุจุฉ ู
ุน ุงูุงุชููู delegate instances.
Program.cs
using System;
namespace DelegatesRealTimeExample
{
class Program
{
static void Main(string[] args)
{
// Create delegate instances pointing to our callback methods
WorkPerformedHandler del1 = new WorkPerformedHandler(Worker_WorkPerformed);
WorkCompletedHandler del2 = new WorkCompletedHandler(Worker_WorkCompleted);
Worker worker = new Worker();
// Pass the delegate instances to the DoWork method
worker.DoWork(5, "Generating Report", del1, del2);
Console.ReadKey();
}
// Callback method for progress reporting
static void Worker_WorkPerformed(int hours, string workType)
{
Console.WriteLine($"{hours} Hours completed for {workType}");
}
// Callback method for completion notification
static void Worker_WorkCompleted(string workType)
{
Console.WriteLine($"{workType} work completed");
}
}
}ุฏูููุชูุ ุดุบู ุงูุชุทุจูู ููุชูุงูู ุฅูู ูู ู
ุฑุฉ ุงูู loop ุจุชุชููุฐุ ุจูุจุนุช ุฅุดุนุงุฑุ ููู
ุง ุงูุดุบู ูุฎูุตุ ุจูุจุนุช ุฅุดุนุงุฑ ุชุงูู.
Output:
1 Hours completed for Generating Report
2 Hours completed for Generating Report
3 Hours completed for Generating Report
4 Hours completed for Generating Report
5 Hours completed for Generating Report
Generating Report work completed
ููู ููู ู
ุดููุฉ. ุฌูู Worker classุ ู
ู
ูู ูุฎูู ุงูู delegate instance ุจู null:
public void DoWork(...)
{
del1 = null; // This is allowed
del2 = null; // This is also allowed
// ... code will crash here ...
}ููุงุ ูู Worker class ู
ุด ุจูุนู
ู instance ููู Worker class ู
ุนูุฏูุด ููุฑุฉ ุนู ุงูู methods ุงููู ููุจุนุชููุง ุงูุฅุดุนุงุฑุงุชุ ูุงูู
ูุฑูุถ ู
ูุณู
ุญุด ุจุฅูู ูุฎูู delegate instance ูุจูู null. ุฅุฒุงู ููุฏุฑ ูู
ูุน ุฏูุ ุจุงุณุชุฎุฏุงู
Events ูู C# ูุฏู ุงููู ููุชููู
ุนูู ูู ู
ูุงููุง ุงูุฌุงู.
Delegate Real-Time Example 2: Employee Promotion
ุฏูููุชูุ ููุดูู ู
ุซุงู ุชุงูู ูุงูุนู ููู delegates. ุฎูููุง ูููู ุฅู ุนูุฏูุง class ุงุณู
ู Employee.
namespace DelegateRealtimeExample
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public int Experience { get; set; }
public int Salary { get; set; }
}
}ุฏูููุชู ุฃูุง ุนุงูุฒ ุฃูุชุจ method ูู Employee class ู
ู
ูู ูุณุชุฎุฏู
ูุง ุนุดุงู ูุฑูู ุงูู
ูุธููู. ุงูู method ุงููู ูููุชุจูุง ูุชุงุฎุฏ ูุงูู
ุฉ ู
ู Employee objects ูู parameter ูุจุนุฏูู ุงูู
ูุฑูุถ ุชุทุจุน ุฃุณู
ุงุก ูู ุงูู
ูุธููู ุงููู ู
ุคูููู ููุชุฑููุฉ.
ููู ุงูู
ูุทู ุงููู ุนูู ุฃุณุงุณู ุงูู
ูุธู ุจูุชุฑูู ุงูู
ูุฑูุถ ู
ููููุด hard coded. ุฃููุงุช ู
ู
ูู ูุฑูู ุงูู
ูุธููู ุจูุงุกู ุนูู ุฎุจุฑุชูู
ูุฃููุงุช ุชุงููุฉ ู
ู
ูู ูุฑูููู
ุจูุงุกู ุนูู ู
ุฑุชุจูู
ุฃู ูู
ูู ุจูุงุกู ุนูู ุณุจุจ ุชุงูู ุฒู ุฃุฏุงุก ุงูู
ูุธู.
ุฅุฒุงู ููุฏุฑ ูุญูู ุฏูุ ุฏู ู
ู
ูู ูุญููู ุจุณูููุฉ ุฌุฏูุง ุจู
ุณุงุนุฏุฉ delegates. ุนุดุงู ูุฏูุ ู
ุญุชุงุฌ ุฃุตู
ู
Employee class ุจุชุงุนู ุฒู ู
ุง ูู ู
ูุถุญ ุชุญุช:
using System;
using System.Collections.Generic;
namespace DelegateRealtimeExample
{
// Delegate to check promotion eligibility
public delegate bool EligibleToPromotion(Employee EmployeeToPromotion);
public class Employee
{
// ... properties
// This method uses the delegate to decide who to promote
public static void PromoteEmployee(List<Employee> lstEmployees, EligibleToPromotion IsEmployeeEligible)
{
foreach (Employee employee in lstEmployees)
{
// The promotion logic is passed in via the delegate
if (IsEmployeeEligible(employee))
{
Console.WriteLine($"Employee {employee.Name} Promoted");
}
}
}
}
}ูู ุงูููุฏ ุงููู ูููุ ุนู
ููุง delegate ุงุณู
ู EligibleToPromotion. ุงูู delegate ุฏู ุจูุงุฎุฏ Employee object ูู parameter ูุจูุฑุฌุน ููู
ุฉ boolean ุจุชูุถุญ ุฅุฐุง ูุงู ุงูู
ูุธู ุงุชุฑูู ููุง ูุฃ. ูู Employee classุ ุนู
ููุง ูู
ุงู PromoteEmployee method ูุงูู method ุฏู ุจุชุงุฎุฏ ูุงูู
ุฉ Employees ู Delegate ู
ู ููุน EligibleToPromotion ูู parameters.
ุจุนุฏููุ PromoteEmployee method ุจุชูู ุนูู ูู employee object ูุชู
ุฑุฑู ููู delegate. ูู ุงูู delegate ุฑุฌุน trueุ ูุจูู ุงูู
ูุธู ุงุชุฑููุ ูุฅูุง ูุฃ. ูุฌูู ุงูู methodุ ู
ุนู
ููุงุด hardcode ูุฃู ู
ูุทู ุนู ุฅุฒุงู ุนุงูุฒูู ูุฑูู ุงูู
ูุธููู.
ุฏูููุชู ุงูุนู
ูู (client) ุงููู ุจูุณุชุฎุฏู
Employee class ุนูุฏู ุงูู
ุฑููุฉ ุฅูู ูุญุฏุฏ ุงูู
ูุทู ุจุชุงุน ุฅุฒุงู ุนุงูุฒ ูุฑูู ุงูู
ูุธููู ุจุชูุนู.
Program.cs
using System;
using System.Collections.Generic;
namespace DelegateRealtimeExample
{
public class Program
{
static void Main()
{
// Create employees...
List<Employee> lstEmployess = new List<Employee>();
// Add employees to the list...
// Create a delegate instance pointing to our promotion logic method
EligibleToPromotion eligibleToPromote = new EligibleToPromotion(Program.PromoteBySalary);
Employee.PromoteEmployee(lstEmployess, eligibleToPromote);
Console.ReadKey();
}
// This is the specific promotion logic we want to use
public static bool PromoteBySalary(Employee employee)
{
if (employee.Salary > 10000)
{
return true;
}
else
{
return false;
}
}
}
}ูุงุญุธ PromoteBySalary method ุงููู ุนู
ููุงูุง. ุงูู method ุฏู ูููุง ุงูู
ูุทู ุจุชุงุน ุฅุฒุงู ุนุงูุฒูู ูุฑูู ู
ูุธูููุง. ุงูู method ุจุนุฏูู ุจุชู
ุฑุฑ ูู parameter ููู delegate. ูุงุญุธ ูู
ุงู ุฅู ุงูู method ุฏู ูููุง ููุณ ุงูู signature ุจุชุงุน EligibleToPromotion delegate. ุฏู ู
ูู
ุฌุฏูุง ูุฅู PromoteBySalary method ู
ูููุนุด ุชุชู
ุฑุฑ ูู parameter ููู delegate ูู ุงูู signature ู
ุฎุชูู. ุฏู ูู ุงูุณุจุจ ุงููู ุจูุฎูู ุงูู delegates ุชุชุณู
ู type-safe function pointers.
ูู ู
ูุงูุด ุนูุฏูุง ู
ูููู
delegatesุ ู
ููุด ููุจูู ู
ู
ูู ูู
ุฑุฑ function ูู parameter.
ูู C# 3.0ุ ุชู
ุชูุฏูู
Lambda expressions. ูุชูุฏุฑ ุชุณุชุฎุฏู
lambda expressions ุจุฏู ู
ุง ุชุนู
ู function ูุจุนุฏูู instance ู
ู delegate. ุงูู
ุซุงู ุงูุชุงูู ู
ูุชูุจ ุชุงูู ุจุงุณุชุฎุฏุงู
Lambda expression. ุงูู PromoteBySalary method ู
ุจูุงุด ู
ุทููุจ ุฏูููุชู.
// Inside Main method
// The lambda expression defines the promotion logic inline
Employee.PromoteEmployee(lstEmployess, x => x.Experience > 5);ุฏู ููุฑูู ุฃู ู ูุธู ุฎุจุฑุชู ุฃูุชุฑ ู ู 5 ุณููู.