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ุ ูู
ุง ูุงูุชุงูู:
FuncActionPredicate
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ููุฏุฑ ูุงุฎุฏ ูุญุฏ 16input 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ููุฏุฑ ูุญุชูู ุนูู 16input parameterูุญุฏ ุฃูุตู ููุงุฒู ูููู ูููreturn typeูุงุญุฏ ููู ุขุฎุฑparameter. - ุงูู
Actionููุฏุฑ ูุญุชูู ุนูู 16input parameterูุญุฏ ุฃูุตู ูู ุนูุฏูุด ุฃูreturn type. - ุงูู
Predicateูุงุฒู ูุญูู ู ุนุงููุฑ ู ุนููุฉ ููmethodููุงุฒู ูููู ูููinput parameterูุงุญุฏ ุจุณ. ุจุดูู ุงูุชุฑุงุถูุ ูููoutput parameterูุงุญุฏ ู ู ููุนreturnูู ุด ู ุญุชุงุฌูู ูู ุฑุฑoutput parameterูููPredicate.