Searching...

Translate

Using delegate in C#

Ø 2.6.1.2 Using delegate in C#

   Below program illustrates how a delegate is created and used in a program.

using System;

delegate double del_calc(double x,double y);



class Program
    {
        public static double calc(double x,double y)
        {
          
            Console.WriteLine("Addition of given no. is as
 below");
            return (x + y);

       }

        static void Main(string[] args)
        {
            double ans;
            del_calc c=new del_calc(calc);
            ans = c(13, 12);
            Console.WriteLine("Addition = {0}", ans);
            Console.ReadLine();
        }
    }


   Output
Addition of given no. is as below
A is = 25


Ø 2.6.1.3 Simple delegate example


using System;

delegate int delcalc(double a,double b);

class Program
{
      public static int metadd(int a,int b)
      {     return(a+b)       }
      public static int metsub(int a,int b)
      {     return(a-b)       }
      static void Main()
      {
            delcalc d1=new delcalc(metadd);
            int add=d1(13,12);
            Console.WritLine(“Addition = {0}”,add);

delcalc d2=new delcalc(metsub);
int sub=d2(8,7);
Console.WriteLine(“Subtraction = {0}”,sub);
                  }
            }

                 
           
   Output
Addition = 25
Subtraction = 1


ü Multicast delegates


   We have seen so far that a delegate can invoke only one method. However, it is possible for certain delegates to hold and invoke multiple methods. Such delegates are called multicast delegates. Multicast delegates, also known as combinable delegates, must satisfy the following conditions:

§  The return type of the delegate must be void.
§  None of the parameters of the delegate type can be declared as output parameters, using out keyword

   If D is delegate that satisfies the above conditions and d1,d2,d3 and d4 are the instances of D, then the statements

D3 = d1+d2; //d3 refers to two methods
D4 = d3-d2; //d4 refers to only d1 method

   Are valid provided that the delegate instances d1 and d2 have already been initialized with method references and d3 and d4 contain null reference.

   Below program illustrates the application of multicast delegates.

using System;

    delegate void multicast();
    class Program
    {
        public static void Display()
        {
            Console.WriteLine("New Delhi");
        }
        public static void Print()
        {
            Console.WriteLine("New Yourk");
        }
        static void Main(string[] args)
        {
            multicast m1 = new multicast(Display);
            multicast m2 = new multicast(Print);
            multicast m3 = m1 + m2;
            multicast m4 = m3 - m2;
            multicast m5 = m4 + m2;
            m3();
            m4();
            m5();
            Console.ReadLine();
        }
    }


   Output

New delhi
New yourk
New delhi
New delhi
New yourk







*      2.6.2 Anonymous method

   You will often find that the method referred to by a delegate is used only for that purpose. In other words, the only reason for the method is so it can be invoked via a delegate.

   The method is never called on its own. In such a case, you can avoid the need to create a separate method by using an anonymous function.

   Anonymous method is a way to pass code block as a delegate parameter that is associated with a specific delegate instance.

   The body of the method is written “inline” the source code of the method is passed as a parameter to the instance of the delegate.

   Anonymous method can have arguments and return type. Example of all the type of anonymous method is described as below.

Þ   Example :- Simple anonymous method

using System;

delegate void display();

class Program
{
    static void Main(string[] args)
    {
        display d = delegate
        {
            for (int i = 1; i <= 5; i++)
            {
                Console.WriteLine(i);
            }
        };
        d();
        Console.ReadLine();
    }
}

   Output
1
2
3
4
5

Þ   Example: - Anonymous method with parameters only.

using System;

delegate void display(int end);

class Program
{

    static void Main(string[] args)
    {
        display d = delegate(int end)
        {
            for (int i = 1; i <= end; i++)
            {
                Console.WriteLine(i);
            }
        };
        d(7);
        Console.ReadLine();

    }
}

   Output
1
2
3
4
5
6
7


Þ   Example: - Anonymous method with parameters and Return type.

using System;
delegate int count(int end);

class Program
static void Main(string[] args)
      {
          count c = delegate(int end)
          {
              int sum = 0;   
              for (int i = 1; i <= end; i++)
              {
                  sum = sum + i;
              }
              return sum;
          };
        
            int ans1 = c(5);
            int ans2 = c(10);
            Console.WriteLine("Sum of first 5 no. is =
                                          {0}", ans1);
            Console.WriteLine("Sum of First 10 no. is =
                                          {0}", ans2);
            Console.ReadLine();
         
        }
    }

   Output:

Sum of first 5 no. is = 15
Sum of first 10 no. is = 55


0 comments:

Post a Comment

Subscribe us