In this article we are going to discuss how to implement delegates with examples in C#.
Example
Companies send their delegates to conferences to represent their products(because CEO might be busy with other things).
The CEO defines certain responsibilities to delegates (in advance)
Now the CEO select one delegate for the conference when needed (say at run time).
1. Single Delegate
Steps to declare and use a delegate in C#
To call a method by using a delegate we have three steps. Here is a basic example of how to declare and use a delegate in C#:
1. Declaring a delegate
This declaration defines a delegate named AddDelegate that can reference any method that takes a two int parameter and returns void.
public delegate void AddDelegate(int a, int b);
2. Defining methods that match the delegate signature
Now the point is delegate and function type and parameter should be same type. That is the reason why delegate say type safe function pointer.
public void Add(int a, int b)
{
Console.WriteLine(a + b);
}
3. Instantiating and using the delegate
AddDelegate ad = new AddDelegate(prog.Add);
ad(10, 30)
//ad.Invoke(20, 30);
Example
using System;
namespace delegateproject
{
public delegate void AddDelegate(int a, int b);
public delegate string SayDelegate(string str);
class Program
{
public void Add(int a, int b)
{
Console.WriteLine(a + b);
}
public static string SayHello(string name)
{
return "Hello " + name;
}
static void Main()
{
Program prog = new Program();
AddDelegate ad = new AddDelegate(prog.Add);
ad(10, 30);
ad.Invoke(20, 30);
SayDelegate sd = new SayDelegate(Program.SayHello);
string str = sd("Rohatash");
string str1 = sd.Invoke("Rahul");
Program.SayHello(str);
Console.WriteLine(str);
Console.WriteLine(str1);
Console.ReadLine();
}
}
}
Output
It holds the references of more than one method. When a multicast delegate is invoked, it calls each of its assigned methods in the order they were added. To combine the delegates we can use the + operator.
Let's see a scenario where we have a Facebook class that publishes notifications, and Mobile and Email classes that receive these notifications. We'll implement this using multicast delegates.
Example
using System;
namespace delegateproject
{
public delegate void NotificationHandler(string message);
public class Publisher
{
public static void PublishNotification(string message, NotificationHandler notify)
{
Console.WriteLine($"Facebook: Publishing notification: {message}");
notify?.Invoke(message);
}
}
public class Mobile
{
// Method to handle the notification
public void ReceiveNotification(string message)
{
Console.WriteLine($"Mobile: Received notification: {message}");
}
}
public class Email
{
// Method to handle the notification
public void ReceiveNotification(string message)
{
Console.WriteLine($"Email: Received notification: {message}");
}
}
public class Program
{
public static void Main()
{
Publisher Publisher = new Publisher();
// Create instances of the subscribers
Mobile mobile = new Mobile();
Email email = new Email();
// Create a multicast delegate
NotificationHandler notify = null;
// Add methods to the multicast delegate
notify += mobile.ReceiveNotification;
notify += email.ReceiveNotification;
// Invoke the multicast delegate
Publisher.PublishNotification("New post from your friend!", notify);
Publisher.PublishNotification("New message received!", notify);
// Remove a method from the multicast delegate
notify -= mobile.ReceiveNotification;
// Invoke the multicast delegate again
Publisher.PublishNotification("Mobile unsubscribed, another post from your friend!", notify);
}
}
}
Output
The above example demonstrates how to use multicast delegates to invoke multiple methods with a single delegate. It shows how to add methods to a delegate, invoke the delegate, and remove methods from the delegate.