Introduction

ููŠ ุงู„ู…ู‚ุงู„ ุฏู‡ุŒ ู‡ู†ุชูƒู„ู… ุนู† ุงู„ู€ Generic Delegates ููŠ C# ุจุงู„ุฃู…ุซู„ุฉ. ูŠุง ุฑูŠุช ุชู‚ุฑุง ู…ู‚ุงู„ู†ุง ุงู„ู„ูŠ ูุงุช ุงู„ู„ูŠ ู†ุงู‚ุดู†ุง ููŠู‡ ุฃู…ุซู„ุฉ ูˆุงู‚ุนูŠุฉ ู„ู„ู€ Delegate ููŠ C#. ูƒุฌุฒุก ู…ู† ุงู„ู…ู‚ุงู„ ุฏู‡ุŒ ู‡ู†ุชูƒู„ู… ุนู† ุงู„ู†ู‚ุท ุงู„ู…ู‡ู…ุฉ ุฏูŠ ุจุงู„ุชูุตูŠู„:

  • ุฅูŠู‡ ู‡ูŠ ุงู„ู€ Generic Delegates ููŠ C#ุŸ
  • ุฃู†ูˆุงุน ุงู„ู€ Generic Delegates ููŠ C#.
  • ู„ูŠู‡ ุจู†ุญุชุงุฌ Generic DelegatesุŸ
  • ุงู„ู€ Func Generic Delegate ููŠ C#.
  • ุงู„ู€ Action Generic Delegate ููŠ C#.
  • ุงู„ู€ Predicate Generic Delegate ููŠ C#.
  • ุฃู…ุซู„ุฉ ุนุดุงู† ู†ูู‡ู… Generic Delegates.

What are Generic Delegates in C#?

ุงู„ู€ Generic Delegates ููŠ C# ุชู… ุชู‚ุฏูŠู…ู‡ุง ูƒุฌุฒุก ู…ู† .NET Framework 3.5 ูˆู‡ูŠ ู…ุด ุจุชุญุชุงุฌ ุฅู†ูƒ ุชุนุฑู‘ู delegate instance ุนุดุงู† ุชุณุชุฏุนูŠ ุงู„ู€ methods. ุนุดุงู† ุชูู‡ู… Generic DelegatesุŒ ู„ุงุฒู… ูŠูƒูˆู† ุนู†ุฏูƒ ู…ุนุฑูุฉ ุฃุณุงุณูŠุฉ ุจุงู„ู€ Delegates.

Types of Generic Delegates in C#

ุงู„ู€ C# ุจุชูˆูุฑ ุชู„ุงุชุฉ built-in generic delegatesุŒ ู‡ู…ุง ูƒุงู„ุชุงู„ูŠ:

  • Func
  • Action
  • Predicate

Why do we need the Generic Delegates in C#?

ุฎู„ูˆู†ุง ู†ูู‡ู… ุงู„ุญุงุฌุฉ ู„ู€ Generic Delegates ุจู…ุซุงู„. ุงู„ุฃูˆู„ุŒ ุฎู„ูˆู†ุง ู†ูู‡ู… ุฅุฒุงูŠ ุจู†ุณุชุฎุฏู… delegates ุนุดุงู† ู†ุณุชุฏุนูŠ methods. ุฎู„ูˆู†ุง ู†ู‚ูˆู„ ุฅู† ุนู†ุฏู†ุง ุงู„ุชู„ุงุช methods ุฏูˆู„ ูˆุนุงูŠุฒูŠู† ู†ุณุชุฏุนูŠู‡ู… ุจุงุณุชุฎุฏุงู… delegates.

// Method 1: Takes three parameters and returns a double
public static double AddNumber1(int no1, float no2, double no3) { /* ... */ }
 
// Method 2: Takes three parameters and returns void
public static void AddNumber2(int no1, float no2, double no3) { /* ... */ }
 
// Method 3: Takes one string and returns a boolean
public static bool CheckLength(string name) { /* ... */ }

ุฏู„ูˆู‚ุชูŠุŒ ู„ูˆ ุนุงูŠุฒูŠู† ู†ุณุชุฏุนูŠ ุงู„ุชู„ุงุช methods ุงู„ู„ูŠ ููˆู‚ ุฏูˆู„ ุจุงุณุชุฎุฏุงู… delegatesุŒ ูŠุจู‚ู‰ ู„ุงุฒู… ู†ุนู…ู„ ุชู„ุงุชุฉ delegates ุงู„ู€ signatures ุจุชุงุนุชู‡ู… ุชุทุงุจู‚ ุงู„ู€ signatures ุจุชุงุนุฉ ุงู„ู€ methods ุฏูŠ:

// Delegate for AddNumber1
public delegate double AddNumber1Delegate(int no1, float no2, double no3);
// Delegate for AddNumber2
public delegate void AddNumber2Delegate(int no1, float no2, double no3);
// Delegate for CheckLength
public delegate bool CheckLengthDelegate(string name);

ูˆุจู…ุฌุฑุฏ ู…ุง ุจู†ุนู…ู„ delegatesุŒ ู†ู‚ุฏุฑ ู†ุณุชุฏุนูŠ ุงู„ู€ methods ุจุฅู†ู†ุง ู†ุนู…ู„ instances ู…ู† ูƒู„ delegate ุจุชุดูŠุฑ ู„ู„ู€ method ุจุชุงุนุชู‡ุง:

AddNumber1Delegate obj1 = new AddNumber1Delegate(AddNumber1);
double Result = obj1.Invoke(100, 125.45f, 456.789);

Do we really need to create Custom Delegates to Invoke Methods in C#?

ุงู„ุฅุฌุงุจุฉ ู‡ูŠ ู„ุฃ. ุงู„ู€ .NET Framework ุจูŠูˆูุฑ ุดูˆูŠุฉ generic delegates ุชู‚ุฏุฑ ุชู‚ูˆู… ุจุงู„ูˆุธูŠูุฉ ุฏูŠ ู„ูŠู†ุง. ุงู„ู€ C# ุจุชูˆูุฑ ุชู„ุงุชุฉ Generic Delegatesุ› ู‡ู…ุง: Func, Action, Predicate.

What is Func Generic Delegate in C#?

ุงู„ู€ Func Generic Delegate ู…ูˆุฌูˆุฏ ููŠ System namespace. ุงู„ู€ delegate ุฏู‡ ุจูŠุงุฎุฏ input parameter ุฃูˆ ุฃูƒุชุฑ ูˆุจูŠุฑุฌุน out parameter ูˆุงุญุฏ. ุขุฎุฑ parameter ุจูŠุนุชุจุฑ ู‚ูŠู…ุฉ ุงู„ุฅุฑุฌุงุน (return value).

  • ุงู„ู€ Func Generic Delegate ูŠู‚ุฏุฑ ูŠุงุฎุฏ ู„ุญุฏ 16 input parameters ู…ู† ุฃู†ูˆุงุน ุจูŠุงู†ุงุช ู…ุฎุชู„ูุฉ ุฃูˆ ุฒูŠ ุจุนุถ.
  • ู„ุงุฒู… ูŠูƒูˆู† ู„ูŠู‡ return type ูˆุงุญุฏ. ุงู„ู€ return type ุฅุฌุจุงุฑูŠ ู„ูƒู† ุงู„ู€ input parameter ู…ุด ุฅุฌุจุงุฑูŠ.

ู…ู„ุงุญุธุฉ: ูƒู„ ู…ุง ุงู„ู€ delegate ุจุชุงุนูƒ ูŠุฑุฌุน ู‚ูŠู…ุฉุŒ ุณูˆุงุก ูƒุงู† ุจูŠุงุฎุฏ input parameter ุฃูˆ ู„ุฃุŒ ู„ุงุฒู… ุชุณุชุฎุฏู… Func Generic delegate.

What is Action Generic Delegate in C#?

ุงู„ู€ Action Generic Delegate ู…ูˆุฌูˆุฏ ุจุฑุถู‡ ููŠ System namespace. ู‡ูˆ ุจูŠุงุฎุฏ input parameter ุฃูˆ ุฃูƒุชุฑ ูˆู…ุจูŠุฑุฌุนุด ุฃูŠ ุญุงุฌุฉ (void). ุงู„ู€ delegate ุฏู‡ ูŠู‚ุฏุฑ ูŠุงุฎุฏ ุจุญุฏ ุฃู‚ุตู‰ 16 input parameters.

ู…ู„ุงุญุธุฉ: ูƒู„ ู…ุง ุงู„ู€ delegate ุจุชุงุนูƒ ู…ูŠุฑุฌุนุด ุฃูŠ ู‚ูŠู…ุฉุŒ ุณูˆุงุก ูƒุงู† ุจูŠุงุฎุฏ input parameter ุฃูˆ ู„ุฃุŒ ูŠุจู‚ู‰ ู„ุงุฒู… ุชุณุชุฎุฏู… Action Generic delegate.

What is Predicate Generic Delegate in C#?

ุงู„ู€ Predicate Generic Delegate ู…ูˆุฌูˆุฏ ุจุฑุถู‡ ููŠ System namespace. ุงู„ู€ delegate ุฏู‡ ุจูŠูุณุชุฎุฏู… ุนุดุงู† ูŠุชุญู‚ู‚ ู…ู† ู…ุนุงูŠูŠุฑ ู…ุนูŠู†ุฉ ู„ู„ู€ method ูˆุจูŠุฑุฌุน ุงู„ู€ output ูƒู‚ูŠู…ุฉ BooleanุŒ ูŠุง True ูŠุง False.

  • ุจูŠุงุฎุฏ input parameter ูˆุงุญุฏ.
  • ุฏุงูŠู…ู‹ุง ุจูŠุฑุฌุน ู‚ูŠู…ุฉ Boolean ูˆุฏู‡ ุฅุฌุจุงุฑูŠ.

ู…ู„ุงุญุธุฉ: ูƒู„ ู…ุง ุงู„ู€ delegate ุจุชุงุนูƒ ูŠุฑุฌุน ู‚ูŠู…ุฉ BooleanุŒ ุจูŠุงุฎุฏ input parameter ูˆุงุญุฏ ุจุณุŒ ูŠุจู‚ู‰ ู„ุงุฒู… ุชุณุชุฎุฏู… Predicate Generic delegate.

Examples to understand Generic Delegates in C#

ุฎู„ูˆู†ุง ู†ุณุชุฎุฏู… generic delegates ุนุดุงู† ู†ุณุชุฏุนูŠ ุงู„ุชู„ุงุช methods ุจุชูˆุนู†ุง:

How to use Func Generic Delegate in C#?

AddNumber1 method ุจุชุงุฎุฏ input ูˆุจุชุฑุฌุน output. ูุงู„ู€ signature ุจุชุงุนู‡ุง ุจูŠุทุงุจู‚ Func generic delegate.

// Func<input1_type, input2_type, ..., return_type>
Func<int, float, double, double> obj1 = new Func<int, float, double, double>(AddNumber1);
double Result = obj1.Invoke(100, 125.45f, 456.789);

ุงู„ู€ Func ู‡ู†ุง ุจูŠุงุฎุฏ ุฃุฑุจุนุฉ parametersุŒ ุฃูˆู„ ุชู„ุงุชุฉ ู‡ู…ุง input parameters ูˆุงู„ุขุฎุฑ ู‡ูˆ return value.

How to use Action Generic Delegate in C#?

AddNumber2 method ุจุชุงุฎุฏ input ู„ูƒู† ู…ุจุชุฑุฌุนุด ุฃูŠ ู‚ูŠู…ุฉ. ูุงู„ู€ signature ุจุชุงุนู‡ุง ุจูŠุทุงุจู‚ Action generic delegate.

// Action<input1_type, input2_type, ...>
Action<int, float, double> obj2 = new Action<int, float, double>(AddNumber2);
obj2.Invoke(50, 255.45f, 123.456);

How to use Predicate Generic Delegate in C#?

CheckLength method ุจุชุงุฎุฏ input parameter ูˆุงุญุฏ ู…ู† ู†ูˆุน string ูˆุจุชุฑุฌุน ู‚ูŠู…ุฉ Boolean. ูุงู„ู€ signature ุจุชุงุนู‡ุง ุจูŠุทุงุจู‚ Predicate generic delegate.

// Predicate<input_type>
Predicate<string> obj3 = new Predicate<string>(CheckLength);
bool Status = obj3.Invoke("Pranaya");

ุงู„ู…ุซุงู„ ุงู„ูƒุงู…ู„ ุจุงุณุชุฎุฏุงู… Func, Action, Predicate:

using System;
namespace GenericDelegateDemo
{
    public class GenericDelegates
    {
        static void Main(string[] args)
        {
            Func<int, float, double, double> obj1 = new Func<int, float, double, double>(AddNumber1);
            double Result = obj1.Invoke(100, 125.45f, 456.789);
            Console.WriteLine(Result);
 
            Action<int, float, double> obj2 = new Action<int, float, double>(AddNumber2);
            obj2.Invoke(50, 255.45f, 123.456);
 
            Predicate<string> obj3 = new Predicate<string>(CheckLength);
            bool Status = obj3.Invoke("Pranaya");
            Console.WriteLine(Status);
 
            Console.ReadKey();
        }
        public static double AddNumber1(int no1, float no2, double no3) { /* ... */ }
        public static void AddNumber2(int no1, float no2, double no3) { /* ... */ }
        public static bool CheckLength(string name) { /* ... */ }
    }
}

Generic Delegates with Lambda Expression in C#

ุฎู„ูˆู†ุง ู†ุดูˆู ุฅุฒุงูŠ ู†ุณุชุฎุฏู… Lambda Expression ู…ุน Generic Delegates. ููŠ ุงู„ู…ุซุงู„ ุฏู‡ุŒ ุจุฏู„ ู…ุง ู†ู…ุฑุฑ methodsุŒ ู‡ู†ุณุชุฎุฏู… lambda expressions:

using System;
namespace GenericDelegateDemo
{
    public class GenericDelegates
    {
        static void Main(string[] args)
        {
            Func<int, float, double, double> obj1 = (x, y, z) =>
            {
                return x + y + z;
            };
            double Result = obj1.Invoke(100, 125.45f, 456.789);
            Console.WriteLine(Result);
 
            Action<int, float, double> obj2 = (x, y, z) =>
            {
                Console.WriteLine(x + y + z);
            };
            obj2.Invoke(50, 255.45f, 123.456);
 
            // You can also use a method for Predicate if you prefer
            Predicate<string> obj3 = new Predicate<string>(CheckLength);
            bool Status = obj3.Invoke("Pranaya");
            Console.WriteLine(Status);
            Console.ReadKey();
        }
        // ... methods
    }
}

Points to Remember while working with C# Generic Delegates

  • Func, Action, Predicate ู‡ู…ุง Generic Inbuilt delegates ู…ูˆุฌูˆุฏูŠู† ููŠ System namespace.
  • ุงู„ุชู„ุงุชุฉ delegates ุฏูˆู„ ู…ู…ูƒู† ูŠูุณุชุฎุฏู…ูˆุง ู…ุน method, Anonymous Method, ูˆ Lambda Expressions.
  • ุงู„ู€ Func ูŠู‚ุฏุฑ ูŠุญุชูˆูŠ ุนู„ู‰ 16 input parameter ูƒุญุฏ ุฃู‚ุตู‰ ูˆู„ุงุฒู… ูŠูƒูˆู† ู„ูŠู‡ return type ูˆุงุญุฏ ูˆู‡ูˆ ุขุฎุฑ parameter.
  • ุงู„ู€ Action ูŠู‚ุฏุฑ ูŠุญุชูˆูŠ ุนู„ู‰ 16 input parameter ูƒุญุฏ ุฃู‚ุตู‰ ูˆู…ุนู†ุฏูˆุด ุฃูŠ return type.
  • ุงู„ู€ Predicate ู„ุงุฒู… ูŠุญู‚ู‚ ู…ุนุงูŠูŠุฑ ู…ุนูŠู†ุฉ ู„ู€ method ูˆู„ุงุฒู… ูŠูƒูˆู† ู„ูŠู‡ input parameter ูˆุงุญุฏ ุจุณ. ุจุดูƒู„ ุงูุชุฑุงุถูŠุŒ ู„ูŠู‡ output parameter ูˆุงุญุฏ ู…ู† ู†ูˆุน return ูˆู…ุด ู…ุญุชุงุฌูŠู† ู†ู…ุฑุฑ output parameter ู„ู„ู€ Predicate.