CSharp - Multicast Delegates and Chaining Delegates

Introduction

When a delegate encapsulates more than one method of a matching signature, it becomes a multicast delegate.

These delegates are subtypes of System.MulticastDelegate, which is a subclass of System.Delegate.

The following is an example of a multicast delegate.

Demo

using System;

public delegate void MultiDel();

class Program/*ww  w . j a  v  a2s. c o  m*/
{
    public static void show1() { Console.WriteLine("Program.Show1()"); }
    public static void show2() { Console.WriteLine("Program.Show2()"); }
    public static void show3() { Console.WriteLine("Program.Show3()"); }
    static void Main(string[] args)
    {
        MultiDel md = new MultiDel(show1);
        md += show2;
        md += show3;
        md();
    }
}

Result

In general, for a multicast delegate, we have multiple methods in the invocation list.

The return value from Multicast Delegates and Chaining Delegates is from the last method only.

The preceding methods will be called but the return values will be discarded.

Related Topic