ุงูู Delegate ูู (Type-Safe Function Pointer). ุฏู ู
ุนูุงู ุฅู ุงูู delegate ุจูุจูุง ุฌูุงู reference ูู method ุฃู function ููู
ุง ุจูุณุชุฏุนู ุงูู delegateุ ุงูู method ุงููู ุจูุดุงูุฑ ูููุง ุจุชุชููุฐ.
ุงูู delegate signature ูุงูู method ุงููู ุจูุดุงูุฑ ุนูููุง ูุงุฒู
ูููู ูููู
ููุณ ุงูู signature.
ููู ููุนูู ู
ู ุงูู Delegates ูู C#:
- ุงูู Single Cast Delegate: ุงูู
Delegateุจูุดูุฑ ููfunctionุฃูmethodูุงุญุฏุฉ ุจุณ. - ุงูู Multicast Delegate: ุงูู
Delegateุจูุดูุฑ ูุฃูุชุฑ ู ูfunctionุฃูmethod.
ุงุชููู
ูุง ุจุงููุนู ุนู Singlecast Delegate. ุงูููุงุฑุฏูุ ููุชููู
ุนู Multicast Delegates.
What is Multicast Delegate in C#?
ุงูู Multicast Delegate ูู C# ูู delegate ุจูุญู
ู references ูุฃูุชุฑ ู
ู handler function ูุงุญุฏุฉ.
ูู
ุง ุจูุณุชุฏุนู multicast delegateุ ูู ุงูู functions ุงููู ุงูู delegate ุจูุดูุฑ ูููุง ุจุชุชููุฐ. ูู ุนุงูุฒ ุชุณุชุฏุนู ุฃูุชุฑ ู
ู method ุจุงุณุชุฎุฏุงู
delegateุ ูุจูู ูู ุงูู method signatures ูุงุฒู
ุชููู ุฒู ุจุนุถ.
ูู multicast delegate ูู ู
ุฌุฑุฏ (array) ู
ู ุฃูุชุฑ ู
ู pipeline ุฃู ุฃูุชุฑ ู
ู delegate.
ุงูู Delegates ุจุชุชููุฐ ุจููุณ ุงูุชุฑุชูุจ ุงููู ุงุชุญุทุช ุจูู ูู ุงูู invocation list. ุงูู InvocationList ูู ุจุจุณุงุทุฉ Array ู
ู delegates ุฃู pipelines ูู pipeline ูููุง ุจูููู ุงูุจูุงูุงุช ูู method ู
ุฎุชููุฉ.
Example to Understand Multicast Delegate in C#
ู
ู ุบูุฑ delegates.
using System;
namespace MulticastDelegateDemo
{
public class Rectangle
{
public void GetArea(double Width, double Height)
{
Console.WriteLine($"Area is {Width * Height}");
}
public void GetPerimeter(double Width, double Height)
{
Console.WriteLine($"Perimeter is {2 * (Width + Height)}");
}
static void Main(string[] args)
{
Rectangle rect = new Rectangle();
rect.GetArea(23.45, 67.89);
rect.GetPerimeter(23.45, 67.89);
Console.ReadKey();
}
}
}Output:
Area is 1592.1205
Perimeter is 182.68
ูู ุงูู
ุซุงู ุงููู ูููุ ุนู
ููุง instance ู
ู Rectangle class ูุจุนุฏูู ุงุณุชุฏุนููุง ุงูุงุชููู methods.
ุฏูููุชู ุฃูุง ุนุงูุฒ ุฃุนู
ู delegate ูุงุญุฏ ูุณุชุฏุนู ุงูุงุชููู methods ุงููู ููู ุฏูู. ุงูุงุชููู methods ูููู
ููุณ ุงูู signature ุจุฃุณู
ุงุก ู
ุฎุชููุฉุ ูููุฏุฑ ูุนู
ู delegate ูุงุญุฏ ูุญู
ู ุงูู reference ุจุชุงุน ุงูุงุชููู methods. ููู
ุง ูุณุชุฏุนู ุงูู delegateุ ููุณุชุฏุนู ุงูุงุชููู methods. ูุฏู ุงููู ุจูุชุณู
ู Multicast Delegate.
Multicast Delegate Example in C#
using System;
namespace MulticastDelegateDemo
{
public delegate void RectangleDelegate(double Width, double Height);
public class Rectangle
{
public void GetArea(double Width, double Height) { /* ... */ }
public void GetPerimeter(double Width, double Height) { /* ... */ }
static void Main(string[] args)
{
Rectangle rect = new Rectangle();
RectangleDelegate rectDelegate = new RectangleDelegate(rect.GetArea);
// Use += operator to chain delegates together.
rectDelegate += rect.GetPerimeter;
Delegate[] InvocationList = rectDelegate.GetInvocationList();
Console.WriteLine("InvocationList:");
foreach (var item in InvocationList)
{
Console.WriteLine($" {item}");
}
Console.WriteLine();
Console.WriteLine("Invoking Multicast Delegate:");
rectDelegate(23.45, 67.89);
Console.WriteLine();
Console.WriteLine("Invoking Multicast Delegate After Removing one Pipeline:");
// Removing a method from delegate object
rectDelegate -= rect.GetPerimeter;
rectDelegate.Invoke(13.45, 76.89);
Console.ReadKey();
}
}
}Output:
InvocationList:
MulticastDelegateDemo.RectangleDelegate
MulticastDelegateDemo.RectangleDelegate
Invoking Multicast Delegate:
Area is 1592.1205
Perimeter is 182.68
Invoking Multicast Delegate After Removing one Pipeline:
Area is 1034.1205
ูู ุงูู
ุซุงู ุงููู ูููุ ุนู
ููุง delegate ูุงุญุฏ ุงูู signature ุจุชุงุนู ูู ูู ููุณ ุจุชุงุน ุงูุงุชููู methods GetArea , GetPerimeter.
ุจุนุฏูู ุนู
ููุง instance ู
ู delegate ูุฑุจุทูุง ุงูุงุชููู methods ุจุงุณุชุฎุฏุงู
+= operator.
ุจููุณ ุงูุทุฑููุฉุ ุชูุฏุฑ ุชุณุชุฎุฏู
-= operator ุนุดุงู ุชุดูู function ู
ู ุงูู delegate.
ุจู
ุฌุฑุฏ ู
ุง ุจูุฑุจุท ุงูุงุชููู methods ุจุงูู delegate instance ููู
ุง ุจูุณุชุฏุนู ุงูู delegateุ ุงูุงุชููู methods ุจูุชููุฐูุง.
Another Approach to Creating Multicast Delegates in C#
using System;
namespace MulticastDelegateDemo
{
public delegate void MathDelegate(int No1, int No2);
public class Program
{
// Static Methods
public static void Add(int x, int y) { /*...*/ }
public static void Sub(int x, int y) { /*...*/ }
// Non-Static Methods
public void Mul(int x, int y) { /*...*/ }
public void Div(int x, int y) { /*...*/ }
static void Main(string[] args)
{
Program p = new Program();
MathDelegate del1 = new MathDelegate(Add);
MathDelegate del2 = new MathDelegate(Program.Sub);
MathDelegate del3 = new MathDelegate(p.Mul);
MathDelegate del4 = new MathDelegate(p.Div);
// Use + operator to chain delegates together
MathDelegate del5 = del1 + del2 + del3 + del4;
Console.WriteLine("Invoking Multicast Delegate::");
del5.Invoke(20, 5);
Console.WriteLine();
Console.WriteLine("Invoking Multicast Delegate After Removing one Delegate:");
del5 -= del2;
del5(22, 7);
Console.ReadKey();
}
}
}ููุง ุงุณุชุฎุฏู
ูุง static ู non-static methods. ุนู
ููุง ุฃุฑุจุนุฉ instances ู
ู ุงูู delegate ูุฑุจุทูุง ุงูุฃุฑุจุน methods. ูู ุงูุขุฎุฑุ ุนู
ููุง delegate instance ุฎุงู
ุณ ูุฑุจุทูุง ุจูู ูู ุงูุฃุฑุจุน delegate instances ุจุงุณุชุฎุฏุงู
+ operator. ุฏูููุชูุ ุงูู delegate ุงูุฎุงู
ุณ ุจูู multicast delegate.
Multicast Delegates with Return Type in C#
ุงูู multicast delegate ุจูุณุชุฏุนู ุงูู methods ุจููุณ ุงูุชุฑุชูุจ ุงููู ุงุชุถุงูุช ุจูู. ูุญุฏ ุฏูููุชูุ ุงูุฃู
ุซูุฉ ุงููู ูุงูุดูุงูุง ู
ูุงูุชุด ุจุชุฑุฌุน ุฃู ุญุงุฌุฉ (void). ุฅูู ุงููู ุจูุญุตู ูู ุงูู delegate ููู return typeุ
ูู ุงูู delegate ููู return type ุบูุฑ voidุ ููู ุงูู delegate ุฏู multicast delegateุ ูุจูู ุจุณ ููู
ุฉ ุขุฎุฑ method ุชู
ุงุณุชุฏุนุงุคูุง ูู ุงููู ูุชุฑุฌุน.
ุจููุณ ุงูู
ูุทูุ ูู ุงูู delegate ููู out parameterุ ูููู
ุฉ ุงูู output parameter ูุชููู ุงูููู
ุฉ ุงููู ุฎุตุตุชูุง ุขุฎุฑ method ุชู
ุงุณุชุฏุนุงุคูุง ู
ู ุงูู Invocation List.
Example to Understand Multicast Delegates with Return Type
using System;
namespace MulticastDelegateDemo
{
// Delegate's return type is int
public delegate int SampleDelegate();
public class Program
{
static void Main()
{
SampleDelegate del = new SampleDelegate(MethodOne);
del += MethodTwo;
// The value returned will be 2, from MethodTwo()
int ValueReturnedByDelegate = del();
Console.WriteLine($"Returned Value = {ValueReturnedByDelegate}");
Console.ReadKey();
}
public static int MethodOne()
{
Console.WriteLine("MethodOne is Executed");
return 1;
}
public static int MethodTwo()
{
Console.WriteLine("MethodTwo is Executed");
return 2;
}
}
}Output:
MethodOne is Executed
MethodTwo is Executed
Returned Value = 2
Example to Understand Multicast Delegates using out Parameter in C#
using System;
namespace MulticastDelegateDemo
{
// Delegate has an int output parameter
public delegate void SampleDelegate(out int Integer);
public class Program
{
static void Main()
{
SampleDelegate del = new SampleDelegate(MethodOne);
del += MethodTwo;
// The value of ValueFromOutPutParameter will be 2
int ValueFromOutPutParameter = -1;
del(out ValueFromOutPutParameter);
Console.WriteLine($"Returned Value = {ValueFromOutPutParameter}");
Console.ReadKey();
}
public static void MethodOne(out int Number)
{
Console.WriteLine("MethodOne is Executed");
Number = 1;
}
public static void MethodTwo(out int Number)
{
Console.WriteLine("MethodTwo is Executed");
Number = 2;
}
}
}Output:
MethodOne is Executed
MethodTwo is Executed
Returned Value = 2