Delegates in C#

Delegates is a type used to hold a reference of method in an object. Delegates can also be referred as a pointer to function. Delegates are similar to function pointer in c#.

Syntax of Declaration of Delegates:

Public delegate type_of_delegate delegate_name( );

 
Example

public delegate void d();

Note

Delegate may contain any number of argument or it may be without any parameter list.

Syntax of Creating Instance of Delegate:
 

public delegate_name name_of_instance;

Example

public d obj;

Program
 

using System;

namespace del

{

    class Program

    {

        public delegate int d(int a);

        public static d obj;

        public static int method(int i)

        {

            Console.WriteLine("Creating Method");

            return i;

        }

        static void Main(string[] args)

        {

            int n;

            obj = new d(method);

            n = obj(8);

            Console.WriteLine("value called via delegate is "+n);

            Console.ReadLine();

        }

    }

}

Output

delegate-1.PNG

Description of above Program

  1. In the above program The Delegate "d" is declared with int return type having one parameter.
  2. After that the delegate instance "obj" is created and then a method is created.

    Note
    - Following Syntax should be followed (if you are declaring a method of int type having one parameter of type int, then delegate which you are declaring should be in same format this is the reason it is called type safe reason)
     
  3. In main method the method name is passed to delegate instance.
     

    obj = new d(method);

Multicast Delegate

Multicast Delegate is a Delegate which hold the reference of more than one method.

Note- Multicast Delegate must contain method that returns void. 

Program

using System;

namespace del

{

    class Program

    {

        public delegate void d(int a,int b);

        public static d obj;

        public static void sum(int i, int i1)

        {

            int c;

            c = i + i1;

            Console.WriteLine("Creating Method 1 and value is "+c);

        }

        public static void mul(int i,int i1)

        {

            int c;

            c = i * i1;

            Console.WriteLine("Creating Method 2 and value is " + c);

        }

        static void Main(string[] args)

        {

            obj = new d(sum);

            obj += new d(mul);

            obj(7, 6);

            Console.ReadLine();

        }

    }

}

Output

delegate-2.PNG

Generic Delegate

In this a delegate can define its own parameter.

Syntax of declaration of Generic Delegate:
 

public delegate void d<t>(t a);
 

Syntax of creation of instance of Generic Delegate:
 

d<int> obj = new d<int>(m1);

 
Syntax of calling:

obj(7);

Program

using System;

namespace del

{

    class Program

    {

        public delegate void d<t>(t a);

        public static void m1(int i)

        {

            Console.WriteLine("Creating Method 1 and value is " + i);

        }

        public static void m2(string s)

        {

            Console.WriteLine("Creating Method 2 and message is " + s);

        }

        static void Main(string[] args)

        {

            d<int> obj = new d<int>(m1);

            d<string> obj1 = new d<string>(m2);

            obj(7);

            obj1("hii");

            Console.ReadLine();

        }

    }

}

Output

delegate-3.PNG

Anonymous Method in Delegate

Anonymous methods provide a technique to pass a code block as a delegate parameter. Anonymous methods are basically methods without a name, just the body.

Syntax
 

public delegate void d (int a);

d obj = delegate(int a)

{

   Console.WriteLine("value passed is" + a);

};

 
Program
 

using System;

namespace del

{

    class Program

    {

        public delegate void d (int a);

        static void Main(string[] args)

        {

            d obj = delegate(int a)

            {

                Console.WriteLine("value passed is " + a);

            };

            obj(10);

            Console.ReadLine();

        }

    }

}

Output

delegate-4.PNG

Advantage of Delegate

  1. Improve Performance of Application.
  2. Call function Asynchronously.
  3. Encapsulate the Method Call from Caller.  
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now