Delegates when to use

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tshad

    Delegates when to use

    I am still trying to find out why to use a delegate and when it is overkill.

    For example:

    If I do something like:

    *************** *************** *************** *****
    using System;
    using System.Collecti ons.Generic;
    using System.Text;

    public delegate double Delegate_Prod(i nt a, int b);

    class Class1
    {


    static double fn_Prodvalues(i nt val1, int val2)
    {
    return val1 * val2;
    }
    static void Main(string[] args)
    {


    //Creating the Delegate Instance
    Delegate_Prod delObj = new Delegate_Prod(f n_Prodvalues);


    Console.Write(" Please Enter Values");

    int v1 = Int32.Parse(Con sole.ReadLine() );
    int v2 = Int32.Parse(Con sole.ReadLine() );

    //use a delegate for processing

    double res = delObj(v1, v2);
    Console.WriteLi ne("Result :" + res);
    Console.ReadLin e();

    }
    }
    *************** *************** *************** *****

    I understand why this works. But why would you do this if you can just do:

    *************** *************** *************** *******
    using System;
    using System.Collecti ons.Generic;
    using System.Text;

    public delegate double Delegate_Prod(i nt a, int b);

    class Class1
    {


    static double fn_Prodvalues(i nt val1, int val2)
    {
    return val1 * val2;
    }
    static void Main(string[] args)
    {


    //Creating the Delegate Instance
    Delegate_Prod delObj = new Delegate_Prod(f n_Prodvalues);


    Console.Write(" Please Enter Values");

    int v1 = Int32.Parse(Con sole.ReadLine() );
    int v2 = Int32.Parse(Con sole.ReadLine() );

    //use a delegate for processing

    double res = fn_Prodvalues(v 1, v2); <---------
    Console.WriteLi ne("Result :" + res);
    Console.ReadLin e();

    }
    }
    *************** *************** *************** *******
    Here I do exactly the same thing, except I call the function directly as
    opposed to having to set up a delegate, create the instance and then call
    it.

    If I were looking at this 2nd example what would cause me to think that I
    should use a delegate instead and do the 1st example.

    I understand the mechanics, but I am trying to see why I should do one over
    the other.

    Thanks,

    Tom


  • Jon Skeet [C# MVP]

    #2
    Re: Delegates when to use

    On Jun 20, 5:32 am, "tshad" <t...@dslextrem e.comwrote:
    I am still trying to find out why to use a delegate and when it is overkill.
    <snip>
     Here I do exactly the same thing, except I call the function directly as
    opposed to having to set up a delegate, create the instance and then call
    it.
    >
    If I were looking at this 2nd example what would cause me to think that I
    should use a delegate instead and do the 1st example.
    You shouldn't. Delegates aren't particularly useful when you know
    exactly which method you want to call at the point where you want to
    call it.

    They're useful like generics - where you can write a routine which
    needs to call something but it doesn't particularly care what.
    Enumerable.Wher e is a good example of this: it needs to have something
    to call - a predicate - to determine whether or not a given item in
    the source iterator should appear in the result iterator.

    It doesn't need to know what the predicate does, just that it will
    look at an item and return true or false.

    If you were writing something like the Where method yourself for some
    reason and you already knew what the predicate was, you could put it
    in directly. The beauty of the Where method (etc) is that all the
    logic *except* the predicate is encapsulated in one place, so you
    *only* need to supply the bit that varies (the predicate).

    Jon

    Comment

    • Duggi

      #3
      Re: Delegates when to use

      On Jun 20, 9:32 am, "tshad" <t...@dslextrem e.comwrote:
      I am still trying to find out why to use a delegate and when it is overkill.
      >
      For example:
      >
      If I do something like:
      >
      *************** *************** *************** *****
      using System;
      using System.Collecti ons.Generic;
      using System.Text;
      >
      public delegate double Delegate_Prod(i nt a, int b);
      >
      class Class1
      {
      >
          static double fn_Prodvalues(i nt val1, int val2)
          {
              return val1 * val2;
          }
          static void Main(string[] args)
          {
      >
              //Creating the Delegate Instance
              Delegate_Prod delObj = new Delegate_Prod(f n_Prodvalues);
      >
              Console.Write(" Please Enter Values");
      >
              int v1 = Int32.Parse(Con sole.ReadLine() );
              int v2 = Int32.Parse(Con sole.ReadLine() );
      >
              //use a delegate for processing
      >
              double res = delObj(v1, v2);
              Console.WriteLi ne("Result :" + res);
              Console.ReadLin e();
      >
          }}
      >
      *************** *************** *************** *****
      >
      I understand why this works.  But why would you do this if you can justdo:
      >
      *************** *************** *************** *******
      using System;
      using System.Collecti ons.Generic;
      using System.Text;
      >
      public delegate double Delegate_Prod(i nt a, int b);
      >
      class Class1
      {
      >
          static double fn_Prodvalues(i nt val1, int val2)
          {
              return val1 * val2;
          }
          static void Main(string[] args)
          {
      >
              //Creating the Delegate Instance
              Delegate_Prod delObj = new Delegate_Prod(f n_Prodvalues);
      >
              Console.Write(" Please Enter Values");
      >
              int v1 = Int32.Parse(Con sole.ReadLine() );
              int v2 = Int32.Parse(Con sole.ReadLine() );
      >
              //use a delegate for processing
      >
              double res = fn_Prodvalues(v 1, v2);                  <---------
              Console.WriteLi ne("Result :" + res);
              Console.ReadLin e();
      >
          }}
      >
      *************** *************** *************** *******
       Here I do exactly the same thing, except I call the function directly as
      opposed to having to set up a delegate, create the instance and then call
      it.
      >
      If I were looking at this 2nd example what would cause me to think that I
      should use a delegate instead and do the 1st example.
      >
      I understand the mechanics, but I am trying to see why I should do one over
      the other.
      >
      Thanks,
      >
      Tom
      =============== =============== =============== =============== =
      Good that you tried understanding the delegate concept

      See the following code

      public delegate double Delegate_Prod(i nt a, int b);

      // My Company is the provider of Exm class
      public class Exm
      {
      public Delegate_Prod delObj = null; // delegate object is
      null

      public void ProcessMyLogic( )
      {
      Console.Write(" Please Enter Values\n");

      int v1 = Int32.Parse(Con sole.ReadLine() );
      int v2 = Int32.Parse(Con sole.ReadLine() );

      double res = 0;

      if (delObj != null)
      res = delObj(v1, v2); // I do not actually know how
      to process the data. The data processing is left to the user of the
      class
      else
      throw new Exception("Data Processing Logic is not
      available");

      Console.WriteLi ne("Result :" + res);
      }
      }


      // Other company uses my class for their development
      public class Program
      {
      static double fn_Prodvalues(i nt val1, int val2)
      {
      //class user will decide this logic
      return val1 * val2;
      }

      static double MyOtherLogic(in t val1, int val2)
      {
      //class user will decide this logic
      return val1 - val2;
      }

      public static void Main(string[] args)
      {
      Exm ex = new Exm();

      // ex.delObj = new Delegate_Prod(f n_Prodvalues); // try
      uncomment this line and comment next line
      ex.delObj = new Delegate_Prod(M yOtherLogic);

      ex.ProcessMyLog ic();

      Console.ReadLin e();
      }
      }
      =============== =============== =============== =============== ===========
      In this case I will not be bothering about how the data should be
      processed, instead I left the choice to the user of my class. This is
      the real advantage of delegates.

      These are also useful in events


      -Cnu

      Comment

      Working...