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.
The delegate is just like any other method in its syntax apart from the word delegate is present e.g.
If you create a basic method that will implement a delegate asCode: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);
This is just like any other method, but you can now attach the method that implements a compareCode:public static int StringCompare(object object1, object object2) { .... }
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
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.Code:Compare comp = new Compare(StringCompare)
Here is some example code in full
and the output would beCode:using System; namespace codecalldelegate { /* 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")); } } }
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.Code:Compare Ian against Genux : 1 Compare Genux against Ian : -1 Compare Genux against Genux : 0
CodingfriendsCode:int coffeePerDay = 10; // need to cut down!!!
Cool you had read my mind I was going to do a tutorial on events next![]()
CodingfriendsCode:int coffeePerDay = 10; // need to cut down!!!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks