WebTrainingRoom Online Courses
Learn C# programming Language, Free C# Tutorial for Beginners, C# Development Guide

Learn Delegate and Multicast Delegate in C# .Net

You learn Delegate in C# today
A 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.

[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 implanting multicast delegate in C# to solve a real-time business requirement

Business requirement
Requirement is in an organization when any financial report generated, 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;
}
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#");            
    }
}

Comment
Name Email Website
Subscribe
 
Delegates in C#
in C# Tutorials
Learn C# Online Tutorial


Multicast Delegate Example
Consulting