Use Multicasting Delegates in C# Language

Introduction

Multitasking of delegates is very useful property of delegates , you can create invocation list of methods that will be called a delegate is invoked. Show in the example. 

Example

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

delegate int change_number(int i);         //create delegate change number

namespace using_multicasting_delegates_in_c_Sharp

{

 

    class TestDelegate                     //create class test delegate

    {

        static int number = 15;            //static number declare

        public static int Add(int k)       //create static function add

        {

            number += k;

            return number;

        }

 

        public static int Multiply(int l)   //create static function Multiply

        {

            number *= l;

            return number;

        }

        public static int display()         //create static function display

        {

            return number;

        }

 

        static void Main(string[] args)

        {

            change_number cn;                            //create delegate instances

            change_number cn2 = new change_number(Add);

            change_number cn3 = new change_number(Multiply);

            cn = cn2;

            cn += cn3; 

            cn(10);                   //calling multicast

            Console.WriteLine("Value of Num: {0}", display());

            Console.ReadKey();

        }

    }

}


Output:


output.jpg

Ebook Download
View all
Learn
View all