Use of Delegates in C#

Delegates

Delegates are objects that contain information about the function rather than data. We can encapsulate a reference to the method inside the delegate object. They are similar to function pointers in C and C++ but the difference between delegates and function pointers is that delegates are type safe.

Some points

  1. The return type and signature of  delegates should be the same as the function we want to encapsulate.
  2. It is not necessary to use delegates with parameters; we can use delegates with parameters and without parameters.
  3. We can use static or nonstatic functions; in the case of static functions we encapsulate the function with the class name and in the case of a nonstatic function we first make the object of the class and  then we use a function with the object.

Example of delegate

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Delegates

{
    public
delegate int my_delegate(int a,int b);

    class Program

    {

        static void Main(string[] args)

        {

            my_delegate del = new my_delegate(abc.ss);

            abc obj = new abc();

            my_delegate del1 = new my_delegate(obj.ss1);

            int a = del(4, 2);

            int b = del1(4, 2);

            Console.WriteLine("the result of ss is {0} and the result of ss1 is {1}", a, b);

        }

    }

    class abc

    {

        public static int ss(int i, int j)

        {

            return i * j;

        }

        public int ss1(int x, int y)

        {

            return x + y;

        }

    }

}

OUTPUT

delegate1inc_sharp.png


Multicasting with Delegates

We can call more than one function using the delegate. When that is done it is called multicasting. For this we use the operators "+=" and "-=". We use "+=" to add a method in the invocation list and the "-=" operator to remove a method from the invocation list.

Note:  The return type of the multicast delegate should be void.

Example of multicast delegates.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace multicast_delegates

{

    public delegate void my_multi_delegate(int x,int y);        //declaring a multi cast delegate with return type void

    class Program

    {

        static void Main(string[] args)

        {

            my_multi_delegate del = new my_multi_delegate(abc.ss); //encapsulate the fun ss with the delegate

            del += new my_multi_delegate(abc.ss1); //adding fun ss1 in the invocation list with the help of += operator

            del += new my_multi_delegate(abc.ss2); //adding fun ss2 in the invocation list

            del(4, 2);

            //now we remove the function ss from the invocation lit with the help of -= operator

            del -= new my_multi_delegate(abc.ss); // Now in the invocation list 2 method namely ss1 and ss2 remains

            Console.WriteLine("");

            Console.WriteLine("After removing first fun ss from the invocation list ");

            del(4, 2);

            //now we remove the function ss2 from the invocation lit with the help of -= operator

            del -= new my_multi_delegate(abc.ss2); // Now in the invocation list 1 method ss1 remains

            Console.WriteLine("");

            Console.WriteLine("After removing fun ss2 from the invocation list ");

            del(4, 2);

        }

    }

    class abc

    {

        public static void ss(int a, int b)

        {

            Console.WriteLine("the out put of first method is " + (a + b));

        }

        public static void ss1(int i, int j)

        {

            Console.WriteLine("the out put of second method is " + (i * j));

        }

        public static void ss2(int m, int n)

        {

            Console.WriteLine("the output of 3rd function is " + (m / n));

        }

    }

}

 

OUTPUT

delegatesinc-sharp.jpg

 I hope you now can understand delegates well.

Up Next
    Ebook Download
    View all
    Learn
    View all