Wednesday, June 12, 2013

(Part 2) What are delegates in c#?

Hello everyone, today we are going to proceed from our Previous tutorial, in this there was a introduction for delegates. Now today we are going to discuss multicast delegates.  Following we will do in our today's tutorial
  1. Introduction to multicast delegates
  2. Limitations of multicast delegates
  3. Syntax of multicast delegates
  4.   Implementing multicast delegates
  5. Brief example of multicast delegates
1. Introduction to multicast delegates
     A special feature of delegates is that a single delegate can encapsulate more than one method of matching signature. These kind of delegates are called ‘Multicast Delegates’. Internally, multicast delegates are sub-type of System.MulticastDelegate, which itself is a sub class of System.Delegate

2.  Limitations of multicast delegates
      While using the multicast delegates, the return type of the delegates must be the ‘void’. The reason for this limitation is that a multicast delegate may have multiple methods in its invocation list. Since single delegate (or method) can only return a single value, a multicast delegate type must have the void as the return type.

3.  Syntax of multicast delegates
     The syntax of the multicast delegate is same as the simple delegate, with the limitation which we have discussed earlier:

Delegate void testMulticastDelegate(list of parameters);

Delegate: the keyword that will ensure us that this is the delegate.
Void: the return type of the delegate.
testMulticastDelegate: the name of the delegate.
list of parameters: the matching parameters same like of function.
We will proceed with the following example throughout this tutorial.

Delegate void testMulticastDelegate(int a,int b);

 4. Implementing multicast delegates


     As we have discussed the syntax of our multicast delegate and now we will proceed with the implementation of the multicast delegate or the way referring to more than one function using the same delegate.
Don’t forget to use ‘+=’ operator to refer more than one function into multicast delegate. As we are doing in the following:
testMulticastDelegate arithMethod;
arithMethod  =new testMulticastDelegate(add);
arithMethod  +=new testMulticastDelegate(sub);
arithMethod  +=new testMulticastDelegate(mul);
Don’t forget to use ‘-=’ operator to remove a function from delegate. As we are doing in the following:
testMulticastDelegate arithMethod;
arithMethod  =new testMulticastDelegate(add);
arithMethod  +=new testMulticastDelegate(sub);
arithMethod  -=new testMulticastDelegate(mul);


5. Brief example of multicast delegates
     Open Visual studio and follow the following steps:
File->New->Project->Installed Templates->C#->Windows->Console Application
And paste the following code.




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

namespace ConsoleApplication1
{
    //declaring delegate
    public delegate void testMulticastDelegate(int p, int q);
    class Program
    {
        // function for adding two numbers
        static void add(int a, int b )
        {
            Console.WriteLine("{0} + {1} = {2}",a,b,a+b);
        }

        // function for comparing two numbers
        static void max(int a, int b)
        {
            if (a > b)
            {
                Console.WriteLine("{0} is greater than {1}", a, b);
            }
            else
            {
                Console.WriteLine("{0} is greater than {1}", b, a);
            }
        }
       
        // function for multiplying two number
        static void mul(int a, int b)
        {
            Console.WriteLine("{0} * {1} = {2}", a, b, a * b);
        }
        static void Main(string[] args)
        {
            int num1, num2, num3;
            testMulticastDelegate arithMethod;
           
            // referring more than one function to a delegate
            arithMethod = new testMulticastDelegate(add);
            arithMethod += new testMulticastDelegate(max);
            arithMethod += new testMulticastDelegate(mul);

            Console.Write("Enter first Number:\t");
            num1 = Convert.ToInt16(Console.ReadLine());

            Console.Write("Enter seconed Number:\t");
            num2 = Convert.ToInt16(Console.ReadLine());

            arithMethod(num1,num2);

            Console.Write("Enter 1 for removing multiplication function and 2 for sum:\t");
            num3 = Convert.ToInt16(Console.ReadLine());

            switch(num3)
            {
                case 1:
                    Console.WriteLine("multiplication() method has been removed");
                  
                    // Eliminating a function reference
                    arithMethod -= new testMulticastDelegate(mul);
                    arithMethod(num1, num2);
                    break;
                case 2:
                    Console.WriteLine("sum() method has been removed");

                  
                     // Eliminating a function reference
                    arithMethod -= new testMulticastDelegate(add);
                    arithMethod(num1, num2);
                    break;

            }
            Console.ReadLine();
        }
    }
}








This will out put in the following way.


T
In the end of today's tutorial we have understand the data encapsulation in OOP.
That's all from today.
Thank You! ! !

 


No comments:

Post a Comment