Showing posts with label Delegates in C#. Show all posts
Showing posts with label Delegates in C#. Show all posts

Tuesday, July 22, 2014

Delegates in C#

Delegates can be used to hold the reference of method. A delegate is a type that safety encapsulates a method and it is similar to function pointer in C and C++. Delegates can be used to define call back methods. Delegate class hold only those methods which have same signature. It allow methods to be passed as parameters. A delegate can be seen as a placeholder for method.
Delegates can be chained multiple methods together and can be called on a single event.
Suppose if you have multiple methods with same signature which have same return type & number of parameters and want to call all the methods with single object then we can go for delegates.
Creating and using delegates involve the four steps:
1. Declaring a delegates
2. Defining a delegate method
3. Creating delegate object
4. Invoking delegate object
    class Program
    {
        // Step-1. Declaring a delegates
        // Access modifier - delegate keyword - return type - delegate-name
        public delegate void Calculate(int a, int b);

        // Step-2. Defining a delegate method
        // Signature of method exactly matches the delegate signature.
        public static void Add(int x, int y)
        {
            Console.WriteLine("Sum = {0}", x + y);
        }

        static void Main(string[] args)
        {
            // Step-3. Creating delegate object
            Calculate calculate = new Calculate(Add);
            // Step-4. Invoking delegate object
            calculate(5, 5);
            Console.ReadKey();
        }
       
    }
You can also assign any method to degates like :
// Step-3. Creating delegate object
  Calculate calculate = Add;

Types of Delegates


1. Single cast delegate

Delegate that represent only a single method is known as Single Cast Delegate.
    class Program
    {
        //  Declaring a delegates
       
        public delegate void Calculate(int a, int b);

        // Defining a delegate method
       
        public static void Add(int x, int y)
        {
            Console.WriteLine("Sum = {0}", x + y);
        }

        static void Main(string[] args)
        {
            // Creating delegate object
            Calculate calculate = new Calculate(Add);
            // Invoking delegate object
            calculate(5, 5);
            Console.ReadKey();
        }
       
    }

2. Multi Cast Delegate

Delegate that represent more than one method is known as Multi Cast Delegate.
Use += operator to add reference of more method to delegate
Use -= operator to remove reference of a method
    class Program
    {
        // Declaring a delegates       
        public delegate void Calculate(int a, int b);

        // Defining a delegate method       
        public static void Add(int x, int y)
        {
            Console.WriteLine("Sum = {0}", x + y);
        }

        // Defining second delegate method
        public static void Multiply(int x, int y)
        {
            Console.WriteLine("Multiply = {0}", x + y);
        }
  
        static void Main(string[] args)
        {
            // Creating delegate object
            Calculate calculate = new Calculate(Add);
            // Add second method to delegate
            calculate += Multiply;

            // Invoking delegate object, it will call Add and Multiply both method
            calculate(5, 5);

            Console.ReadKey();
        }
   }
You can also create separate delegate object for each method and combine all objects to a single to perform Multicast delegate.
   // Alternate way of creating Multicast delegate object
   Calculate delegate1 = Add;
   Calculate delegate2 = Multiply;
   Calculate calculate = delegate1 + delegate2;
Remove reference of a method from Multicast delegate object.
        static void Main(string[] args)
        {
            //Creating delegate object
            Calculate calculate = new Calculate(Add);
            // add second method to delegate
            calculate += Multiply;

            // Remove reference of Add method to delegate
            calculate -= Add;
            // Invoking delegate object, it will call only Multiply method
            calculate(5, 5);

            Console.ReadKey();
        }

        // Output
        // Multiply = 10