Friday, May 7, 2021

C# Delegate

DELEGATES


As we know that we can pass any datatype argument to a method. But what if we want to pass a function as argument to the method and then what can be its datatype? We will have to specify the datatype of passed function when passed as an argument of the function. How can we get the datatype of the function? Datatype of function doesn't mean its return type. The datatype in this case will be of delegate type. 
Consider the following methods and their signatures:

public void Method1(int x, int y) {…}

public void Method2(int a, int b) {…}

public void Method3(int p, int q) {…}

A delegate is a type to denote methods of same signature type. For above methods, we can declare a delegate as follows:

public delegate void DelegateName(int t1, int t2); //declaration of delegate/type

Since delegate is a type, we can create its instance using its constructor. Remember that the name of constructor is the name of the type and parameter of the delegate’s constructor will be a method name. For example,

DelegateName delg = new DelegateName(Method2); //create instance of delegate

OR, 

DelegateName delg = Method2; //create instance of delegate

Here delg is a reference type variable which refers to an instance of delegate named DelegateName. Now to call Method2, we can invoke delg using Invoke() method. The aguments of the method Method2 will be passed via Invoke(). For example, delg.Invoke(2,5).

Therefore there are three steps to use delegates.

  1. Declare a delegate
  2. Pass a compatible method to delegate
  3. Pass compatible arguments of the method using Invoke() In the delegate

 public delegate void DelegateName(int t1, int t2); //declaration of delegate/type

the parameters type is int. 

NOTE: Instead of a named method such as Method1, an anonymous method can also be parameter of delegate. The important point is this that the signature of the anonymous method must be compatible to the delegate's.

What if the datatype of parameters is generic? In this case, generic delegate is declared as show below:

public delegate R DelegateName(S t1, T t2); //declaration of generic delegate. Here, R,S and T can be of any type such as int, float, double etc.

NOTE: In the System namespace, a generic delegate named Func is defined. We can create a delegate of Func type. Func delegate must return a value of any type except void. The methods which return void use Action delegate. It is defined in System namespace.

EXAMPLE

using System;

namespace ConsoleDelegateEx1

{

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

    class Program

    {

        static void Main(string[] args)

        {

            DelegateName delg1 = new DelegateName(Method1);//better delg1 be named product

            delg1.Invoke(2,5);

            DelegateName delg2 = new DelegateName(Method2);//better delg2 be named sum

            delg2.Invoke(2, 5);

            Console.ReadKey();

        }

        private static void Method1(int a, int b)

        {

            Console.WriteLine("Product of {0} and {1} is {2}", a, b, a * b);

        }

        private static void Method2(int a, int b)

        {

            Console.WriteLine("Sum of {0} and {1} is {2}", a, b, a+b);

        }

    }

}

OUTPUT
Product of 2 and 5 is 10
Sum of 2 and 5 is 7

No comments:

Post a Comment

Hot Topics