Latest News
Mar 27, 2017, 6:29 AM
using System;
using System.Collections.Generic;

/* This example shows how you can use delegates in a generic list to call several methods in sequence
* The final output of this tutorial will be:
*
* WriteOut method called
* 2
* WriteIn method called
* 0
*
* As you see, the methods are called in the order they were added to the generic list.
* Also note that the value of the argument is the same for each call, that's because using this
* method, you are supplying the value seperately to each function in the delegate.
*/
namespace DelegatesTutorial
{
class Example1
{
// Declare the delegate to be used later, this sets the return type and expected parameters
// for the delegate. All functions to be included must have the same return type and parameter list.
delegate void myDelegate(int number);
public Example1()
{
// We'll create a generic array of delegates
List TheDelegates = new List();
// Now we'll add the functions to the delegate array list
TheDelegates.Add(new myDelegate(WriteOut));
TheDelegates.Add(new myDelegate(WriteIn));
// Here we'll loop through every delegate in the list, calling it's associated function
foreach (myDelegate simple in TheDelegates)
{
simple(1);
}
}
// These are the functions to be called by the delegate
void WriteOut(int number)
{
Console.WriteLine("WriteOut method called");
int i = ++number;
Console.WriteLine(i.ToString());
}
void WriteIn(int number)
{
Console.WriteLine("WriteIn method called");
int i = --number;
Console.WriteLine(i.ToString());
}
}
}
Mar 27, 2017, 5:50 AM

In C and C++ there were function pointers. Delegates are C#’s object oriented and type safe answer to function pointers. Let’s say you have the following two methods:

public void WriteOut()
{
Console.WriteLine("WriteOut method called");
}
public void WriteIn()
{
Console.WriteLine("WriteIn method called");
}

In your code, you want to call one of these based on the value of the integer selected, you could write it using an if statement like this:

if(selected == 1)
{
WriteOut();
}
elseif(selected == 2)
{
WriteIn();
}
else
{
Console.WriteLine("Hunh?");
}

But what happens if you add more methods later? You’d have to add more else statements, eventually resulting in unwieldy code and really long source files, especially if you have hundreds of such methods.So what’s a better option? Use an array of delegates. Before I show you how to do this, let me show you how to declare and use a delegate to begin with.First we declare the delegate:

delegate void SimpleDelegate();

Now, let’s create an instance of the delegate and assigne the WriteOut method:

SimpleDelegate d = new SimpleDelegate(WriteOut);

Now, instead of calling WriteOut directly, we can call the delegate like this:

d();

Yes, that’s right, we just called the WriteOut method by calling on the delegate d. But how is this handy? Well, think of it this way, now that we have a reference to a method we can create an array containing the references. This is something we couldn’t do with method names. For instance:

delegate void SimpleDelegate();
SimpleDelegate[] void d() = new SimpleDelegate[2]();
d[0]() = SimpleDelegate(WriteIn);
d[1]() = SimpleDelegate(WriteOut);

Now that we’ve setup our delegate array, the next and final piece of the puzzle is to iterate through the array calling each referenced method.

For(int a = 0; a < 2; a++)
{
d[a]();
}

That’s it, we’ve just called both functions from within a for loop. Realistically we’d never loop through the entire array calling each referenced method, instead we’d want to call just one method based on the value of an integer, but this example provides you all the necessary knowledge to get it done.What if we did want to call every method all at once? Well, there’s another option with delegates that we haven’t yet explored. Let’s rewrite our delegate definitions:

delegate void SimpleDelegate();
SimpleDelegate void d() = new SimpleDelegate();
d() = SimpleDelegate(WriteIn);
d() += SimpleDelegate(WriteOut);
d();

In the above code we’ve declared a single delegate. In the second line we assign the WriteIn method to the delegate, and in the third line we add the WriteOut method to the delegate. When we call the delegate in line four it will call the WriteIn method, then it will call the WriteOut method. We’ve now called all the delegate’s methods without having to use a for loop.But what if we later want to remove a method from a delegate? Continuing the previous example we can do it like this:

d() -= SimpleDelegate(WriteOut);

Now the WriteOut method is removed from the delegate, but the delegate still contains a reference to the WriteIn method.There are several rules regarding delegates that you should know of. First, when using a delegate all methods being assigned must have the same parameter list as when you declare the delegate. You can declare a delegate with parameters like:

Delegate int SimpleDelegate(intNumber1, intNumber2);

All methods being assigned to a delegate must also provide the same type return value as the delegate declared. In the preceeding example, all assigned methods must accept two integer parameters and return one integer value.If you call upon a delegate that contains references to two or more methods, the values passed into the parameters are counted as local variables for as long as the delegate runs. If the first method modifies the value of a passed parameter, the modified value is what is passed to the second method, and so on and so forth until all referenced methods have ran.