Monday 11 August 2014

Delegate in C#

Delegate:
C# delegates are similar to pointers to functions, in C or C++. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime.
Delegates are especially used for implementing events and the call-back methods. All delegates are implicitly derived from the System.Delegate class.
Basically delegates in c# are type safe objects which are used to hold reference of one or more methods in c#.net.
Delegates concept will match with pointer concept of c language. 
Suppose if you have multiple methods with same signature (return type & number of parameters)
and want to call all the methods with single object then we can go for delegates.

Single Cast Delegates:
Single cast delegate means which hold address of single method like as explained in above example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Practice
{
    class Program
    {
        static void Main(string[] args)
        {
            Delegate d = new Delegate();
            d.call();
        }

    }
}

public delegate int delegateMethod(int a,int b);
    class Delegate
    {
        public int Add(int x,int y)
        {
            return x + y;
        }
        public int Sub(int x, int y)
        {
            return x - y;
        }
        public void call()
        {
            Delegate dadd = new Delegate();
            delegateMethod d1 = dadd.Add;
            int i = d1(20, 10);
            Console.WriteLine(i);
            Delegate dsub = new Delegate();
            delegateMethod d2 = dsub.Sub;
            int j = d2(30, 10);
            Console.WriteLine( j);
        }

    }

Output
30
20


Multicast Delegates:
Multi cast delegate is used to hold address of multiple methods in single delegate.
To hold multiple addresses with delegate we will use overloaded += operator and 
if you want remove addresses from delegate we need to use overloaded operator -=
Multicast delegates will work only for the methods which have return type only void.
If we want to create a multicast delegate with return type we will get the return type of last method in the invocation list

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Practice
{
    class Program
    {
        static void Main(string[] args)
        {
       multicastdelegate d = new multicastdelegate();
            d.call();
        }

    }
}

public delegate void Multidelegate(int x,int y);
    class multicastdelegate
    {
        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 call()
        {
            multicastdelegate mc = new multicastdelegate();
            Multidelegate md = mc.Add;
            md += mc.Sub;
            md += mc.Mul;
            md(20, 10);
        }
    }

Output
Add 30
Sub 10
Mul 200

No comments:

Post a Comment