Tuesday 12 March 2013

Types of Delegates

Delegates are of two types:
  • Unicast Delegate
  • Multicast Delegate
If a delegate is used for calling a single method we call it as a unicast delegate whereas if a delegate is use for calling multiple methods its a multicast delegate.
In case of the methods delegate, a single delegate call will execute all the methods that are bound with the delegate.
using System;

namespace CSharpConsole
{
    public  delegate void MathDel(int x, int y);
    class DelMulti
    {
        public void Add(int x, int y)
        {
            Console.WriteLine("Add:  " + (x + y));
        }
        public void Sub(int x, int y)
        {
            Console.WriteLine("Sub:  " + (x - y));
        }
        public void Mul(int x, int y)
        {
            Console.WriteLine("mul:  " + (x * y));
        }
        public void Div(int x, int y)
        {
            Console.WriteLine("Div:  " + (x / y));
        }
        static void Main()
        {
            DelMulti obj = new DelMulti();
            MathDel md=new MathDel (obj.Add );
            md+=obj.Sub ;md +=obj.Mul ;md +=obj.Div ;
            md(100,25);
            Console .WriteLine ();
            md(600,30);
            Console .WriteLine ();
            md -=obj.Mul ;
            md(450,50);
            Console .ReadLine ();
            
        }

    }
}
 

 

Delegates in C#

It is also an user defined type which is used for invoking or calling the methods of a class.

A method that is defined under a class can be called in 2 different ways
  • With the help of object of a class if it is non static or name of a class if it is static.
  • By using the delegate also we can call  the method of a class even if it is static or non static.
Note: Calling the method in the 1st process is different than calling the method in the 2nd process.

Calling a method using a Delegate

       If we want to call a method using a delegate we need to adopt the following process
  • Defining a delegate:   
[<modifiers>] delegate <void/type> <Name> ([<Parameter Definition's>])

The definition of a delegate is similar to a method definition where a method will have body whereas a delegate will not have any body where we use these delegates for calling these methods.

Note: While defining a delegate make sure the I/O parameters of Delegates are same as the I/O parameters of the method we want to call with the help of a delegate.
  • As the delegate is a type, after defining a delegate to consume it we need to create an object of it and while creating the object, the method we want to call using the delegate should be passed as a parameter to the delegates constructor.
  • Now call the delegate by passing the required parameter value so that the method gets executed internally.

Delegate Example


using System;
namespace CSharpConsole
{
    class DelDemo
    {
        public void Add(int x, int y)
        {
            Console.WriteLine(x + y);
        }
        public static string SayHello(string name)
        {
            return "Hello " + name;
        }
        public delegate void AddDel(int a, int b);
        public delegate string SayDel(string name);

        static void Main()
        {
            DelDemo obj = new DelDemo();
            AddDel ad = new AddDel(obj.Add);
            SayDel  sd = new SayDel (DelDemo.SayHello);
            ad(100, 50); ad(234, 434); ad(672, 157);
            Console.WriteLine(sd("AAA"));
            Console.WriteLine(sd("BBB"));
            Console.WriteLine(sd("CCC"));
            Console.ReadLine();
                   
        }
    }
}