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.

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); 

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

public static int StringCompare(object object1, object object2)
{
....
}

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

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

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"));
		}
	}
}

and the output would be

Compare Ian against Genux  : 1
Compare Genux against Ian  : -1
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.

This page was published on It was last revised on

0

1 Comment

  • Votes
  • Oldest
  • Latest
Commented
Updated

Might also be helpful tying this in with events 🙂

add a comment
0