C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# Multicast Delegate

In this tutorial, we will discuss multicast delegates in C#. A multicast delegate is a delegate that can have multiple methods assigned to it. When a multicast delegate is invoked, it will call all the assigned methods in the order they were added. Multicast delegates are useful when you want to execute multiple methods in response to a single event or action.

  • Delegate Basics

A delegate is a reference type that represents a method with a specific signature. It allows you to encapsulate a method as an object and pass it around in your code. To declare a delegate, use the delegate keyword followed by the method signature.

Here's a simple delegate declaration:

public delegate void MyDelegate(string message);

In this example, we declare a delegate named MyDelegate that represents a method taking a single string parameter and returning void.

  • Creating Multicast Delegates

To create a multicast delegate, you can use the += operator to combine multiple delegates into one. When the multicast delegate is invoked, it will call all the assigned methods in the order they were added.

Here's an example of creating a multicast delegate:

public class MyClass
{
    public void Method1(string message)
    {
        Console.WriteLine($"Method1: {message}");
    }

    public void Method2(string message)
    {
        Console.WriteLine($"Method2: {message}");
    }
}

class Program
{
    static void Main(string[] args)
    {
        MyClass myClass = new MyClass();
        MyDelegate myDelegate = myClass.Method1;
        myDelegate += myClass.Method2;

        myDelegate("Hello, World!"); // Output: Method1: Hello, World!
                                     //         Method2: Hello, World!
    }
}

In this example, we create an instance of MyClass, and then we create a MyDelegate instance and assign it to myClass.Method1. Next, we use the += operator to add myClass.Method2 to the delegate. When we invoke myDelegate, both Method1 and Method2 are called with the given argument.

  • Removing Methods from Multicast Delegates

To remove a method from a multicast delegate, you can use the -= operator. This will remove the last occurrence of the specified method from the invocation list of the delegate.

Here's an example of removing a method from a multicast delegate:

myDelegate -= myClass.Method1;
myDelegate("Hello, World!"); // Output: Method2: Hello, World!

In this example, we use the -= operator to remove myClass.Method1 from the myDelegate multicast delegate. When we invoke myDelegate, only Method2 is called with the given argument.

  • GetInvocationList Method

You can use the GetInvocationList method to obtain an array of delegates in the invocation list of a multicast delegate. This can be useful if you need to manipulate or inspect the individual delegates.

Here's an example of using the GetInvocationList method:

Delegate[] invocationList = myDelegate.GetInvocationList();

foreach (MyDelegate singleDelegate in invocationList)
{
    singleDelegate("Hello, World!");
}

In this example, we call the GetInvocationList method on myDelegate to obtain an array of individual delegates. We then iterate over the array and invoke each delegate separately.

This tutorial introduced multicast delegates in C#, which are delegates that can have multiple methods assigned to them. When a multicast delegate is invoked, it will call all the assigned methods in the order they were added.

  1. How to use Multicast Delegates in C#

    A multicast delegate can reference multiple methods, and when invoked, it calls all the referenced methods.

    using System;
    
    public delegate void MyDelegate(string message);
    
    class Program
    {
        static void Main()
        {
            MyDelegate multicastDelegate = Method1;
            multicastDelegate += Method2;
    
            // Invoking all methods in the delegate
            multicastDelegate("Hello, Multicast Delegates!");
    
            // Output:
            // Method1: Hello, Multicast Delegates!
            // Method2: Hello, Multicast Delegates!
        }
    
        static void Method1(string message)
        {
            Console.WriteLine($"Method1: {message}");
        }
    
        static void Method2(string message)
        {
            Console.WriteLine($"Method2: {message}");
        }
    }
    
  2. Combining and invoking delegates in C#

    Use the += operator to combine delegates and invoke them to execute the methods they reference.

  3. Adding and removing methods from Multicast Delegates

    Use += to add methods and -= to remove methods from a multicast delegate.

    multicastDelegate += Method3;
    multicastDelegate -= Method2;
    
  4. Chaining events with Multicast Delegates in C#

    Multicast delegates are commonly used in event handling to chain multiple event handlers.

  5. C# Multicast Delegates and event handling

    Events in C# often use multicast delegates for handling multiple subscribers.

    public class EventPublisher
    {
        public event MyDelegate MyEvent;
    }
    
    // Subscribing to the event
    var publisher = new EventPublisher();
    publisher.MyEvent += Method1;
    publisher.MyEvent += Method2;
    
  6. Using Multicast Delegates for callback mechanisms in C#

    Multicast delegates are suitable for implementing callback mechanisms where multiple methods need to be notified.

  7. Multicast Delegates and asynchronous programming in C#

    Multicast delegates can be used in asynchronous programming scenarios to execute multiple asynchronous methods concurrently.

  8. C# Multicast Delegates with lambda expressions

    Lambda expressions can be used with multicast delegates to create concise inline methods.

    multicastDelegate += (message) => Console.WriteLine($"Lambda: {message}");
    
  9. C# Multicast Delegates and exception handling

    If an exception occurs in one method of a multicast delegate, it won't prevent the execution of subsequent methods.

  10. Delegate invocation list in C#

    The GetInvocationList method retrieves an array of the methods in the delegate's invocation list.

    Delegate[] methods = multicastDelegate.GetInvocationList();