Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Delegate Beginner stage

Delegate Beginner stage

Scheduled Pinned Locked Moved C#
helpquestionlearning
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kurangu
    wrote on last edited by
    #1

    Can some one help me out of understanding the delegate. we are calling some method with delegate object. program t1=new program() delegateabc d1 = new delegateabc(t1.Testabc); Here I am calling the method Testabc from class program. I don't understand what is the benefit of calling through delagate I can directly use t1.testabc not going through delagte. can someone help me ou with the difference. t

    R 1 Reply Last reply
    0
    • K kurangu

      Can some one help me out of understanding the delegate. we are calling some method with delegate object. program t1=new program() delegateabc d1 = new delegateabc(t1.Testabc); Here I am calling the method Testabc from class program. I don't understand what is the benefit of calling through delagate I can directly use t1.testabc not going through delagte. can someone help me ou with the difference. t

      R Offline
      R Offline
      Robert Rohde
      wrote on last edited by
      #2

      The code wouldn't call the function. For this you would have to add:

      d1(probablySomeArguments);

      In this simple scenario it doesn't make any sense. Let me try to construct a rather simple case where it could be useful: Lets assume you have an array of numbers which you want to apply a function to each one:

      public void Calculate(int[] numbers) {
      for (int i = 0; i < numbers; i++)
      numbers[i] = numbers[i] * 2;
      }

      This small snippet would double all numbers. But what if several callers want to do different things with those numbers? For this you *could* use a delegate:

      public delegate int NumberCalculaterCallback(int number);

      public void Calculate(int[] numbers, NumberCalculaterCallback callback) {
      for (int i = 0; i < numbers; i++)
      numbers[i] = callback(numbers[i]);
      }

      Now the caller could input any function it likes:

      public class Caller {
      private int Double(int number) {
      return number * 2;
      }

      private int AddOne(int number) {
      return number + 1;
      }

      private int Square(int number) {
      return Math.Pow(number, 2);
      }

      public void Test() {
      int[] numbers = new int[] { 1, 2, 3, 4, 5 };
      Calculate(numbers, new NumberCalculaterCallback(Double));
      Calculate(numbers, new NumberCalculaterCallback(AddOne));
      Calculate(numbers, new NumberCalculaterCallback(Square));
      }
      }

      Calculate now uses functions which it has absolute no clue about. This function could be anywhere, even in a different assembly. It just needs to know about the delegate definition and can then make any processing with the numbers the caller can think of.

      K 1 Reply Last reply
      0
      • R Robert Rohde

        The code wouldn't call the function. For this you would have to add:

        d1(probablySomeArguments);

        In this simple scenario it doesn't make any sense. Let me try to construct a rather simple case where it could be useful: Lets assume you have an array of numbers which you want to apply a function to each one:

        public void Calculate(int[] numbers) {
        for (int i = 0; i < numbers; i++)
        numbers[i] = numbers[i] * 2;
        }

        This small snippet would double all numbers. But what if several callers want to do different things with those numbers? For this you *could* use a delegate:

        public delegate int NumberCalculaterCallback(int number);

        public void Calculate(int[] numbers, NumberCalculaterCallback callback) {
        for (int i = 0; i < numbers; i++)
        numbers[i] = callback(numbers[i]);
        }

        Now the caller could input any function it likes:

        public class Caller {
        private int Double(int number) {
        return number * 2;
        }

        private int AddOne(int number) {
        return number + 1;
        }

        private int Square(int number) {
        return Math.Pow(number, 2);
        }

        public void Test() {
        int[] numbers = new int[] { 1, 2, 3, 4, 5 };
        Calculate(numbers, new NumberCalculaterCallback(Double));
        Calculate(numbers, new NumberCalculaterCallback(AddOne));
        Calculate(numbers, new NumberCalculaterCallback(Square));
        }
        }

        Calculate now uses functions which it has absolute no clue about. This function could be anywhere, even in a different assembly. It just needs to know about the delegate definition and can then make any processing with the numbers the caller can think of.

        K Offline
        K Offline
        kurangu
        wrote on last edited by
        #3

        Robert, Thank you very much Now I understood filly.Thanks again for helping in this concept

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups