3/27/2017
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: 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: 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: Now, let’s create an instance of the delegate and assigne the WriteOut method: Now, instead of calling WriteOut directly, we can call the delegate like this: 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: 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. 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: 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: 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: 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.public void WriteOut()
{
Console.WriteLine("WriteOut method called");
}
public void WriteIn()
{
Console.WriteLine("WriteIn method called");
}
if(selected == 1)
{
WriteOut();
}
elseif(selected == 2)
{
WriteIn();
}
else
{
Console.WriteLine("Hunh?");
}
delegate void SimpleDelegate();
SimpleDelegate d = new SimpleDelegate(WriteOut);
d();
delegate void SimpleDelegate();
SimpleDelegate[] void d() = new SimpleDelegate[2]();
d[0]() = SimpleDelegate(WriteIn);
d[1]() = SimpleDelegate(WriteOut);
For(int a = 0; a < 2; a++)
{
d[a]();
}
delegate void SimpleDelegate();
SimpleDelegate void d() = new SimpleDelegate();
d() = SimpleDelegate(WriteIn);
d() += SimpleDelegate(WriteOut);
d();
d() -= SimpleDelegate(WriteOut);
Delegate int SimpleDelegate(intNumber1, intNumber2);