C# Program to Combine Two Delegates


This is a C# Program to combine two delegates.

Problem Description

This C# Program Combines Two Delegates.

Problem Solution

Here when the multicast delegate is called, it invokes the delegates in the list, in order. Only delegates of the same type can be combined.

Program/Source Code

Here is source code of the C# Program to Combine Two Delegates. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

 * C# Program to Combine Two Delegates
using System;
delegate void dele(string s);
class TestClass
    static void Good(string s)
        System.Console.WriteLine("  Good, {0}!", s);
    static void Morning(string s)
        System.Console.WriteLine("  Morning, {0}!", s);
    static void Main()
        dele firstdel, secondDel, multiDel, multiMinusfirstdel;
        firstdel = Good;
        secondDel = Morning; 
        multiDel = firstdel + secondDel;
        multiMinusfirstdel = multiDel - firstdel;
        Console.WriteLine("Invoking delegate firstdel:");
        Console.WriteLine("Invoking delegate secondDel:");
        Console.WriteLine("Invoking delegate multiDel:");
        Console.WriteLine("Invoking delegate multiMinusfirstdel:");
Program Explanation

This C# program is used to combine two delegates. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Assign good to the firstdel variable and morning secondDel variable. Delegate objects can be composed using the “+” operator. A composed delegate calls the two delegates it was composed from. Only delegates of the same type can be composed.

Using this property of delegates, create an invocation list of methods that will be called when a delegate is invoked.This is called multicasting of a delegate. Here when the multicast delegate is called, it invokes the delegates in the list, in order. Only delegates of the same type can be combined.

Runtime Test Cases
Invoking delegate firstDel:
  Good, A!
Invoking delegate SecondDel:
  Morning, B!
Invoking delegate multiDel:
  Good, C!
  Morning, C!
Invoking delegate multiMinusFirstDel:
  Morning, D!

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.