Thursday, 21 November 2013

Delegates And Events - C#



        In this article we are going to see the delegates and events in c#, A delegate is a class that holds the reference of method with the same signature.we can add many methods as a reference to the delegate. we can add or remove the signature reference.Finally we can execute all methods in sequence by reference.

A Event is a trigger raise by user interaction or action.The object that raises the event is a known as event sender.the object that captures the event and response back is known as event receiver.

In this following example we are going to execute a two method with same signature which accepts the int as input parameter. we are going to add four times Add method and remove one time. so add method is called two times.

once a time we uses the delegate and another time event to execute the methods.



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

namespace DelegatesSample
{
    class Program
    {
        public delegate void PrintOperation(int number);

        private event PrintOperation _printevent;
        public event PrintOperation PrintEvent
        {
            add
            {
                lock (this)
                {
                    _printevent += value;
                }
            }
            remove
            {
                lock (this)
                {
                    _printevent -= value;
                }
            }
        }

        static void Main(string[] args)
        {
            Program ka = new Program();
            Console.WriteLine("/* Delegate */");

            /* Using delegate to handle the methods */
            PrintOperation operation = new PrintOperation(ka.Square);
            operation += Add;
            operation += Add;
            operation += Add;
            operation -= Add;           
            operation(5);

            Console.WriteLine();
            Console.WriteLine("/* Event */");
            /* Using Event to handle the methods */           
            ka._printevent += ka.Square;
            ka._printevent += Add;
            ka._printevent += Add;
            ka._printevent += Add;
            ka._printevent -= Add;
            ka._printevent(5);

            Console.Read();
        }

        static void ka__printevent(int number)
        {
            throw new NotImplementedException();
        }

        void Square(int a)
        {
            a *= a;
            Console.WriteLine(" Time {1} : {0}", a, DateTime.Now.ToString());
        }

        static void Add(int a)
        {
            a += 2;
            Console.WriteLine(" Time {1} : {0}", a, DateTime.Now.ToString());
        }
    }
}


Output:
/* Delegate */
 Time 21-11-2013 23:28:33 : 25
 Time 21-11-2013 23:28:33 : 7
 Time 21-11-2013 23:28:33 : 7

/* Event */
 Time 21-11-2013 23:28:33 : 25
 Time 21-11-2013 23:28:33 : 7

 Time 21-11-2013 23:28:33 : 7


From this article you can find various ways of execute a multiple methods of same signature using delegate and events.

No comments:

Post a Comment