Delegates

  • genux
  • Graduate
  • Graduate
  • User avatar
  • Posts: 106
  • Loc: UK

Post 3+ Months Ago

Delegates



Delegate is basically a function definition that will define how a function will return (the return type int) and the parameters passed to it. It is very similar to a function pointer in C++. The great thing is is that at compile time the compiler does not know what method is actually going to be called until run time.

Basic delegate syntax



The delegate is just like any other method in its syntax apart from the word delegate is present e.g.

Code: [ Select ]
public delegate int functionName(parameters);

// in this example I am going to use a comparison of two objects and return -1 (less than), 1 (greater than), 0 (equal)
public delegate int Compare(object object1, object object2);
  1. public delegate int functionName(parameters);
  2. // in this example I am going to use a comparison of two objects and return -1 (less than), 1 (greater than), 0 (equal)
  3. public delegate int Compare(object object1, object object2);


If you create a basic method that will implement a delegate as

Code: [ Select ]
public static int StringCompare(object object1, object object2)
{
....
}
  1. public static int StringCompare(object object1, object object2)
  2. {
  3. ....
  4. }


This is just like any other method, but you can now attach the method that implements a compare

Attaching the delegate to a method



To attach the "real" method implementation to the delegate you have to create a new instance of the delegate with the actual comparing method as a parameter e.g

Code: [ Select ]
Compare comp = new Compare(StringCompare)


the comp is now linked to the StringCompare method and any call to the comp will just goto the StringCompare method instead, but of course if you had a IntegerCompare method just like the StringCompare you can re-link the comp (Compare) delegate to the IntegerCompare and the compiler does not matter as long as the IntegerCompare returns a integer value.

Example code in full



Here is some example code in full

Code: [ Select ]
using System;

namespace ozzudelegate
{
    /* setup the basics of the compare delegate method the returns are :
         object1 > object2 = 1
         object1 < object2 = -1
         object1 == object2 = 0
    */
    public delegate int Compare(object object1, object object2);
    
    class MainClass
    {
        public static int StringCompare(object object1, object object2)
        {
            string string1 = object1.ToString();
            string string2 = object2.ToString();
            
            if (String.Compare(string1, string2) > 0)
            {
                return 1;
            } else if (String.Compare(string1, string2) <0)
            {
                return -1;
            }
            else
                return 0;
        }

        public static void Main(string[] args)
        {
            // setup a Compare object with the StringCompare method as the method to call
            Compare comp = new Compare(StringCompare);
            
            Console.WriteLine("Compare Ian against Genux : " + comp("Ian","Genux"));
            Console.WriteLine("Compare Genux against Ian : " + comp("Genux","Ian"));
            Console.WriteLine("Compare Genux against Genux : " + comp("Genux", "Genux"));
        }
    }
}
  1. using System;
  2. namespace ozzudelegate
  3. {
  4.     /* setup the basics of the compare delegate method the returns are :
  5.          object1 > object2 = 1
  6.          object1 < object2 = -1
  7.          object1 == object2 = 0
  8.     */
  9.     public delegate int Compare(object object1, object object2);
  10.     
  11.     class MainClass
  12.     {
  13.         public static int StringCompare(object object1, object object2)
  14.         {
  15.             string string1 = object1.ToString();
  16.             string string2 = object2.ToString();
  17.             
  18.             if (String.Compare(string1, string2) > 0)
  19.             {
  20.                 return 1;
  21.             } else if (String.Compare(string1, string2) <0)
  22.             {
  23.                 return -1;
  24.             }
  25.             else
  26.                 return 0;
  27.         }
  28.         public static void Main(string[] args)
  29.         {
  30.             // setup a Compare object with the StringCompare method as the method to call
  31.             Compare comp = new Compare(StringCompare);
  32.             
  33.             Console.WriteLine("Compare Ian against Genux : " + comp("Ian","Genux"));
  34.             Console.WriteLine("Compare Genux against Ian : " + comp("Genux","Ian"));
  35.             Console.WriteLine("Compare Genux against Genux : " + comp("Genux", "Genux"));
  36.         }
  37.     }
  38. }


and the output would be

Code: [ Select ]
Compare Ian against Genux : 1
Compare Genux against Ian : -1
Compare Genux against Genux : 0
  1. Compare Ian against Genux : 1
  2. Compare Genux against Ian : -1
  3. Compare Genux against Genux : 0


Conclusion



Delegates allow for methods to be assigned to different parts to be run at the run time, or you could multicast them which would allow for multiple methods called in order (for example if in a paint program and the user wants to have a circle, filled in, and moved to point x,y within a set macro you could have each action as a delegate multicasted.

I do really like to have any feedback regarding any tutorial/post, just reply or PM me.. glad to help, better to share knowledge.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Might also be helpful tying this in with events :)

Post Information

  • Total Posts in this topic: 2 posts
  • Moderator: Tutorial Writers
  • Users browsing this forum: No registered users and 5 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.