Delegates – csharp

A delegate, is how to reference a method, something like function pointer in c++. One of the main questions of why is there a delegate/function pointer, it is because it gives the developer/user maximum amount of flexibility, lets say that you have a list of functions that you want to call if someone passes you there name and these functions could be, join together with surname, add in there ID code to there class and some other functions at the users decision like what is there car registration etc. well a delegate/function pointer can allow for different methods be called without having a set process at compile time. You could take path a , b or even c. It is harder to test with sometimes, but as long as the test process has a line to follow as well then things are good.

The delegate syntax is like other methods apart from the operative word delegate is just before the return type and there is no code within the body, it is more of a skeleton of a method/function and return type.

Here is the basic delegate syntax.

public delegate int Compare(object object1, object object2);

I have included this within the code below, what the code does is to create 5 names, and there is a sort to order the names with the returned value from the compared method/function (bubble sort). But the Compare function could be changed to order A-Z, Z-A etc.. since you can have different Compare’s methods coded but just alter one line of code to call the new method/function.

Compare compare = new Compare(SimpleDelegateName.CompareNames);

that one.

Anyway, hope that the code/comments make sense and also help to give reasons for the delegate and how it is useful.

using System;
 
namespace SimpleDelegate
{
	// setup a basic delegate to compare two objects.
	// return values = 0 (same), 1 (object1 > object2), -1 (object1 < object2)
	public delegate int Compare(object object1, object object2);
 
	class SimpleDelegateName 
	{
		// has to be a static otherwise when you try to create a instance of Compare (the delegate)
		// it will not have anywhere to point to, since static means create method even if not creating
		// the object
		public static int CompareNames(object obj1, object obj2)
		{
			// conver the objects into strings
			string name1 = obj1.ToString();
			string name2 = obj2.ToString();
 
			if (String.Compare(name2,name1) > 0)
			{
				return 1;
			} else if (String.Compare(name2, name1) < 0)
			{
				return -1;
			} else 
			{
				return 0;
			}
		}
 
		public SimpleDelegateName() 
		{
			name = "";
		}
 
		public SimpleDelegateName(String pName)
		{
			name = pName;
		}
 
		public String returnName()
		{
			return name;
		}
 
		private String name;
	}
 
	class MainClass
	{
		public static void Main(string[] args)
		{
			// create 5 names
			SimpleDelegateName[] names = new SimpleDelegateName[5];
			names[0] = new SimpleDelegateName("Ian");
			names[1] = new SimpleDelegateName("John");
			names[2] = new SimpleDelegateName("Alice");
			names[3] = new SimpleDelegateName("Tom");
			names[4] = new SimpleDelegateName("Katie");
			// they are no pictular order.
 
			// the delegate instance varibale
			// create a new compare object with the CompareNames method within the SimpleDeleteName class
			Compare compare = new Compare(SimpleDelegateName.CompareNames);
 
			// the basic bubble sort, moves 1 word at a time up/down the array
			object temp;
			for (int i = 0; i < names.Length; i++)
			{
				for (int j = 0; j < names.Length; j++)
				{
					Console.WriteLine("DEBUG : i = " + i + " j = " + j 
					                   + " name 1 =  " + names[i].returnName() + " name 2 = " + names[j].returnName() 
					                  	+ " compare " + compare(names[i].returnName(), names[j].returnName()));
					// if the names[i] is greater than names[j] then swap the names around.
					if (compare(names[i].returnName(), names[j].returnName()) >0)
					{
						Console.WriteLine("DEBUG : " +names[i].returnName() + "   " + names[j].returnName());
						temp = names[i];
						names[i] = names[j];
						names[j] = (SimpleDelegateName)temp;
						// swapped..
						Console.WriteLine("DEBUG : " +names[i].returnName() + "   " + names[j].returnName() + " SWAPPED");
					}
				}
			}
 
			// print out the names in alphabetical order
			foreach (SimpleDelegateName n in names)
			{
				Console.WriteLine("Name : " + n.returnName());
			}
		}
	}
}
DEBUG : i = 0 j = 0 name 1 =  Ian name 2 = Ian compare 0
DEBUG : i = 0 j = 1 name 1 =  Ian name 2 = John compare 1
DEBUG : Ian   John
DEBUG : John   Ian SWAPPED
DEBUG : i = 0 j = 2 name 1 =  John name 2 = Alice compare -1
DEBUG : i = 0 j = 3 name 1 =  John name 2 = Tom compare 1
DEBUG : John   Tom
DEBUG : Tom   John SWAPPED
DEBUG : i = 0 j = 4 name 1 =  Tom name 2 = Katie compare -1
DEBUG : i = 1 j = 0 name 1 =  Ian name 2 = Tom compare 1
DEBUG : Ian   Tom
DEBUG : Tom   Ian SWAPPED
DEBUG : i = 1 j = 1 name 1 =  Tom name 2 = Tom compare 0
DEBUG : i = 1 j = 2 name 1 =  Tom name 2 = Alice compare -1
DEBUG : i = 1 j = 3 name 1 =  Tom name 2 = John compare -1
DEBUG : i = 1 j = 4 name 1 =  Tom name 2 = Katie compare -1
DEBUG : i = 2 j = 0 name 1 =  Alice name 2 = Ian compare 1
DEBUG : Alice   Ian
DEBUG : Ian   Alice SWAPPED
DEBUG : i = 2 j = 1 name 1 =  Ian name 2 = Tom compare 1
DEBUG : Ian   Tom
DEBUG : Tom   Ian SWAPPED
DEBUG : i = 2 j = 2 name 1 =  Tom name 2 = Tom compare 0
DEBUG : i = 2 j = 3 name 1 =  Tom name 2 = John compare -1
DEBUG : i = 2 j = 4 name 1 =  Tom name 2 = Katie compare -1
DEBUG : i = 3 j = 0 name 1 =  John name 2 = Alice compare -1
DEBUG : i = 3 j = 1 name 1 =  John name 2 = Ian compare -1
DEBUG : i = 3 j = 2 name 1 =  John name 2 = Tom compare 1
DEBUG : John   Tom
DEBUG : Tom   John SWAPPED
DEBUG : i = 3 j = 3 name 1 =  Tom name 2 = Tom compare 0
DEBUG : i = 3 j = 4 name 1 =  Tom name 2 = Katie compare -1
DEBUG : i = 4 j = 0 name 1 =  Katie name 2 = Alice compare -1
DEBUG : i = 4 j = 1 name 1 =  Katie name 2 = Ian compare -1
DEBUG : i = 4 j = 2 name 1 =  Katie name 2 = John compare -1
DEBUG : i = 4 j = 3 name 1 =  Katie name 2 = Tom compare 1
DEBUG : Katie   Tom
DEBUG : Tom   Katie SWAPPED
DEBUG : i = 4 j = 4 name 1 =  Tom name 2 = Tom compare 0
Name : Alice
Name : Ian
Name : John
Name : Katie
Name : Tom

Leave a Reply

Your email address will not be published. Required fields are marked *