Thursday, March 15, 2012

Delegates in C#



Delegate in C# is similar to a function pointer in C++.A delegate is a type that references a method. Delegate is an object that can refer to a method.
When we are creating delegates, we are creating an object that can hold a reference to a method; it necessarily means that a delegate can invoke the method to which it refers. For declaring a delegate we are using keyword “delegate”.

Syntax for declaring the delegate :
Public delegate <retutnType> <delegateName>(prarameters)

Example : public delegate int  Add(int x,int y)

we can use delegates without parameters or with parameter list.
Delegates are two types :1)Single Cast delegate 
                                      2) Multicast delegate.

Single cast delegate : It is a delegate which holds the reference of one method.
Multicast delegate: It is a delegate which holds the reference of more than one method.

The following Sample program will demonstrate the single cast delegate :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BloggerExamples
{
    //delegate declaration with two parameters integers.
    public delegate int MyDelegate(int x, int y);
    class SingleDelegateExample
    {
        public static int Add(int x, int y)
        {

            return x + y;

        }

        static void Main(string[] args)
        {
            MyDelegate del1 = new MyDelegate(Add);
            int addResult = del1(5, 5);
            Console.WriteLine("5 + 5 = {0}\n", addResult);
            Console.ReadLine();
        }
}

Delegate's multicast means that a delegate object can maintain a list of methods to call, rather than a single method .
if you want to add a method to the invocation list of a delegate object , you simply make use of the overloaded += operator, and if you want to remove a method from the invocation list you make use of the overloaded operator -= .

The following program will demonstrate the Multicast delegate

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


namespace BloggerExamples
{
    //delegate declaration with two parameters integers.
    public delegate void MulticastDelegate(int x, int y);
    class MyMulticastDelegate
    {
        public static void Add(int x, int y)
        {

            Console.WriteLine("You are in Add() Method");

            Console.WriteLine("{0} + {1} = {2}\n", x, y, x + y);

        }

        public static void Multiply(int x, int y)
        {

            Console.WriteLine("You are in Multiply() Method");

            Console.WriteLine("{0} X {1} = {2}", x, y, x * y);

        }

        static void Main(string[] args)
        {
            MulticastDelegate del = new MulticastDelegate(Add);
            del += new MulticastDelegate(Multiply);
            Console.WriteLine("****calling Add() and Multibly() Methods.****\n\n");

            del(5, 5);
            del -= new MulticastDelegate(Add);

            Console.WriteLine("\n\n****Add() Method removed.****\n\n");
            del(5, 5);
            Console.ReadLine();
        }
    }
}

Output :




 

No comments:

Post a Comment