C# Delegate and Multicast Delegate

Delegate, is the reference type variable used as pointer to a function.

In this C# delegate tutorial, you will learn how to design and implement Delegate and Multicast Delegate in C# programming.

What is Delegate C#

A C# Delegate is like a pointer to a function, A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime.

delegate string NotifyUser(string email);
Define a Delegate C#

Here is how you can define a delegate C#, Notice, how to define a delegate with parameter.

[access modifier] delegate [return type] [delegate_name]([paramType paramName])
public delegate string NotifyUser(string email);

While implementing a delegate remember three things declaration, instantiation, and invocation.

C# Delegate Implementation Example

Here you see how to ceate instance of delegate and Invoke the method

namespace WTRConsoleApplication
{
delegate string NotifyUser(string email);
class DelegateExample
{
public static string Send(string email)
{
string _message = string.Format("Mail sent to {0} on {1}",email,DateTime.Now);
return _message;
}
static void Main(string[] args)
{
NotifyUser _email = new NotifyUser(Send);
_email.Invoke("webtrainingroom@gmail.com");
        }
    }
}
Multicast Delegate Example in C#

Multicast Delegate holds the reference of more than one method, or you can say the delegate is a type safe function pointer that can reference any method that has the same signature as that delegate has. that means we can have more than one method matching the signature of delegate.

Now here we see an example of implementing multicast delegate in C# to solve a real-time business requirement

Business requirement

Requirement is, when any financial report generated in an organization, that has to be communicated to three set of people, common user, management group and investor or shareholder, and for each group some additional message also to be appended.

As a solution we have written three methods (UpdateUser, UpdateManagement, UpdateShareHolder) with same signature line and invoke them together using delegate.

//declaring the delegate
delegate string Notification(string content);

class MulticastDelegateExample
{
    public static string UpdateUser(string content)
    {
    string _message = string.Format("Mail sent to User: {0} on {1}", content, DateTime.Now);
    return _message;
    }

    public static string UpdateManagement(string content)
    {string _message = string.Format("Mail sent to Management: {0} on {1}", content, DateTime.Now);
    return _message;
    }

    public static string UpdateShareHolder(string content)
    {
    string _message = string.Format("Mail sent to ShareHolder: {0} on {1}", content, DateTime.Now);
    return _message;
    }
   
}

Now we call all Delegate one by one, then combine them together.

static void Main(string[] args)
{
Notification _delegate1 = new Notification(UpdateUser);
Notification _delegate2 = new Notification(UpdateManagement);
Notification _delegate3 = new Notification(UpdateShareHolder);
Notification multicastDelegate = (Notification)Delegate.Combine(_delegate1, _delegate2, _delegate3);
// invoking together
multicastDelegate.Invoke("We have successfully implemented multicast delegate in C#");            
}

As you can see in above example, you can combine multiple delegates together using Delegate.Combine(_delegate1, _delegate2, _delegate3), and finally call the Invoke method.

You should also read following posts:

 
C# Delegates Example
.Net C# Programming Tutorials
Online Tutorials
C# .net Interview Questions Answers
Multicast Delegate Example
.Net C# Examples | Join .Net C# Course