python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 7, Interview Questions Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
delegate in c# with example

Multicast Delegate Example in C#.Net

What are Multicast delegate, type and use?

The multicast delegate is hold the references of more than one methods within the delegate object using the (+=) operator.
1.      Delegate is added using the (+=) operator. 
2.      Delegate is removed using the (-=) operator. 

Example for Multicast Delegate as following,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MulticastDelegateExample
    public delegate int MulticastDelegateName(int num1, int num2);

    public class MulticastClass

        public static int Add(int num1, int num2)
            return num1 + num2;
        public static int Sub(int num1, int num2)
            return num1 - num2;
        static void Main(string[] args)
            //Create the Add Delegate Instance
            MulticastDelegateName objDelegt = new MulticastDelegateName(Add);
            Console.WriteLine("Please enter numbers!");

            int num1 = Int32.Parse(Console.ReadLine());
            int num2 = Int32.Parse(Console.ReadLine());

            int AddRest = objDelegt(num1, num2);
            Console.WriteLine("Result :" + AddRest);

            //Create the Sub Delegate Instance
            objDelegt += new MulticastDelegateName(Sub);
            int subRest = objDelegt(num1, num2);
            Console.WriteLine("Result :" + subRest);


The Output as given below,

Please enter numbers!
num1 = 20
num2 = 10

Results: Add: 30 and Sub: 10


Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - and
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like Powered by Blogger.
ASK Questions