The Wayback Machine - https://web.archive.org/web/20240728233451/https://www.geeksforgeeks.org/multicast-delegates-in-c-sharp/
Open In App

Multicast Delegates in C#

Last Updated : 10 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

A type-safe function pointer is a delegate. It means that the delegate contains a reference to a method or function, and that when we invoke the delegate, the method to which it refers will be executed. The delegate signature and the method to which it points must both be signed. The method must be passed as a parameter to the delegate function Object() { [native code] } when creating the delegate instance.

In C#, there are two kinds of Delegates. These are:

  • SingleCast Delegate: A single function or method is referred to as a delegate.
  • MultiCast Delegate: Delegate refers to the delegation of multiple functions or methods.

Multicast Delegate in C#

One method reference, which is encapsulated in the delegate, is the only one that it is possible for the delegate to call. These Delegates are known as Multicast Delegates because they have the capacity to hold and invoke multiple methods. 

Multicast Delegates, also referred to as Combinable Delegates, are required to meet requirements such as the Delegate’s return type having to be void. No Delegate type parameter that is declared as an output parameter using no keywords is a Delegate type parameter. Concatenating the invocation lists of the two operands of the addition operation yields a Multicast Delegate instance by joining two Delegates. Delegates are used in the order that they are added.

In C#, a delegate that manages the references to multiple handler functions is known as a multicast delegate. All of the functions that the multicast delegate references will be called when the delegate is called. All method signatures should match if you want to use a delegate to call multiple methods.

Key factors relating to Multicast delegate

  • Every method is called using the FIFO (First in, First out) principle.
  • Methods are added to delegates using the + or += operator.
  • Methods can be eliminated from the delegates list using the – or -= operator.

Example 1:

C#
// Multicast delegate example in C#
using System;

namespace MultDel {
public class Rectangle {
  
   public delegate void RectDelegate(double Width, double Height);
    public void Area(double Width, double Height)
    {
        Console.WriteLine($"Area of rectangle is {Width * Height}.");
    }
    public void Perimeter(double Width, double Height)
    {
        Console.WriteLine($"Perimeter of rectangle is {2 * (Width + Height)}.");
    }
    static void Main(string[] args)
    {
        Rectangle rect = new Rectangle();
        RectDelegate obj = rect.Area;
        obj += rect.Perimeter;
        // We can call area and perimeter by invoking a single delegate.
        obj.Invoke(11.27, 35.75);
    }
}
}

Output

Screenshot-2024-05-13-at-74532-PM

In the above-mentioned illustration, we first created a class instance before calling the two methods. Now I want to create a single delegate that will call the two methods mentioned above (i.e. Area and Perimeter). Due to the fact that the two methods have the same signature but different method names, we can create a single delegate that contains the references to both of the above-mentioned methods. The two methods mentioned above will be called when we invoke the delegate. And when we do that, C# refers to it as a Multicast Delegate.

Conclusion

We can infer from the information above that a multicast delegate in C# is a delegate that controls references to multiple handler functions. When the multicast delegate is called, all of the functions it references are all called as well. If you want to use a delegate to call multiple methods, all of the method signatures must be identical.


Similar Reads

Delegates vs Interfaces in C#
A Delegate is an object which refers to a method or you can say it is a reference type variable that can hold a reference to the methods. Delegates in C# are similar to the function pointer in C/C++. It provides a way which tells which method is to be called when an event is triggered. Example: C/C++ Code // C# program to illustrate Delegates using
3 min read
C# | Delegates
A delegate is an object which refers to a method or you can say it is a reference type variable that can hold a reference to the methods. Delegates in C# are similar to the function pointer in C/C++. It provides a way which tells which method is to be called when an event is triggered. For example, if you click on a Button on a form (Windows Form a
6 min read
C# Tutorial
In this C# (C Sharp) tutorial, whether you’re beginner or have experience with other programming languages, our free C# tutorials covers the basic and advanced concepts of C# including fundamentals of C#, including syntax, data types, control structures, classes, and objects. You will also dive into more advanced topics like exception handling, and
8 min read
ASP.NET Interview Questions and Answer
ASP.NET is a powerful framework for building dynamic web applications, known for its scalability, performance, and integration with the .NET ecosystem, developed by Microsoft that allows developers to build dynamic web applications, websites, and services. It is part of the larger .NET framework and provides a comprehensive programming model for cr
15+ min read
C# Interview Questions and Answers
C# is the most popular general-purpose programming language and was developed by Microsoft in 2000, renowned for its robustness, flexibility, and extensive application range. It is simple and has an object-oriented programming concept that can be used for creating different types of applications. Here, we will provide 50+ C# Interview Questions and
15+ min read
Differences Between .NET Core and .NET Framework
.NET Core is a free open source, a general-purpose development platform for developing modern cloud-based software applications on Windows, Linux, and macOS operating systems. It operates across several platforms and has been revamped to make .NET fast, scalable, and modern. .NET Core is one of Microsoft's big contributions and released under the M
4 min read
Basic CRUD (Create, Read, Update, Delete) in ASP.NET MVC Using C# and Entity Framework
Prerequisites: Download and Install Microsoft SQL Server Management StudioDownload and Setting Up Visual Studio Community Version MVC stands for Model View Controller. It is a design pattern that is employed to separate the business logic, presentation logic, and data. Basically, it provides a pattern to style web application. As per MVC, you can d
6 min read
Program to calculate Electricity Bill
Given an integer U denoting the amount of KWh units of electricity consumed, the task is to calculate the electricity bill with the help of the below charges: 1 to 100 units - [Tex]Rs. 10/unit[/Tex]100 to 200 units - [Tex]Rs. 15/unit[/Tex]200 to 300 units - [Tex]Rs. 20/unit[/Tex]above 300 units - [Tex]Rs. 25/unit[/Tex] Examples: Input: U = 250 Outp
9 min read
Extension Method in C#
In C#, the extension method concept allows you to add new methods in the existing class or in the structure without modifying the source code of the original type and you do not require any kind of special permission from the original type and there is no need to re-compile the original type. It is introduced in C# 3.0. Let us discuss this concept
5 min read
Linked List Implementation in C#
A LinkedList is a linear data structure which stores element in the non-contiguous location. The elements in a linked list are linked with each other using pointers. Or in other words, LinkedList consists of nodes where each node contains a data field and a reference(link) to the next node in the list. In C#, LinkedList is the generic type of colle
6 min read
three90RightbarBannerImg