Delegates in C#


what are Delegates ?

  • In simple words we can say delegates are a .NET object which points to a method that matches its specific signature.
  • Delegates add a safety dimension in handling function pointers in .NET.
  • Delegates are type-safe, object oriented, secure .NET objects which can be used to invoke methods of matching signature.
  • While using delegates it is very much necessary to make sure that the functions which the delegates points has the same number of argument type and same return type. For example if we have a method that takes a single string as a parameter and another method that takes two string parameters, then we need to have two separate delegate type for each method.
  • A delegate can be used to invoke a method, the call to which can only be resolved or determined at runtime.
  • Delegates takes method as parameter
Types of Delegates
  • Single Cast Delegate
  • Multi Cast Delegate
Single Cast Delegate: This is a kind of delegate that can refer to single method at one time.

SingleCast Delegates refer to a single method with matching signature. SingleCast Delegates derive from the System.Delegate class

Multi Cast Delegate: This is a kind of delegates that can refer to multiple methods that have the same signature at one time.

Syntax of Declaring a Delegate:

public delegate returntype delegatename(datatype varname);

Example:

public delegate void dele(int x, int y);

Program using Single Cast Delegate:

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

namespace DelegateEX
{
    public delegate void mydele(int x, int y);

    class A
    {
        public void add(int x, int y)
        {
            Console.WriteLine("The Sum is " + (x + y));
        } 

        public void sub(int x, int y)
        {
            Console.WriteLine("The Difference is " + (x - y));
            Console.ReadKey();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            A obj = new A();
            //make an object of class A
            mydele m = new mydele(obj.add);
            m(10, 20);
            mydele m1 = new mydele(obj.sub);
            m1(10, 20);
        }
    }
}

singleDelegateoutput.bmp

In the above program we created a delegate named mydele having two parameters of type int and is not returning any value. In Main function we created its two objects m and m1 pointing add and sub function of class A respectively.

Now let's discuss the concept of using Multicast delegate in which one delegate points to multiple methods at one time.

Program using Multi Cast Delegate:

using System;
using
System.Collections.Generic;
using
System.Text;
namespace
DelegateEX
{
    public delegate void mydele(int x, int y);

    class A
    {
        public void add(int x, int y)
        {
            Console.WriteLine("The Sum is " + (x + y));
        } 

        public void sub(int x, int y)
        {
            Console.WriteLine("The Difference is " + (x - y));
            Console.ReadKey();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            A b1 = new A();
            //make an object of class A
            myMultiDele Md = new myMultiDele(b1.add);
            myMultiDele Md1 = new myMultiDele(b1.sub);
            Md = Md + Md1;
            Md(60, 90);
        }
    }
}

Output

MultiDelegateoutput.bmp
 


Similar Articles