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 ุณู†ูŠู†.