Let's find out how to use delegates in C# (the easy way). Also, we'll see the difference between simple cast delegates and multicast delegates.
When using delegates in C# we must follow the two steps that we follow when using a class in C#. At first, we need to define it, so that the compiler knows what fields and methods make up the class. After that (unless we use only static methods) we need to instantiate an object of that class.
With delegates we have to start off by defining the delegates that we want to use. By this we tell the compiler what kind of methd a delegate of that type will represent. The second step is to create an instance of that delegate
When defining a delegate we must type something like this:
delegate void SomeOperation(int x); |
In our case, we've just defined a delegate with the name of SomeOperation and we've indicated that an instance of this delegate can hold a reference to a method that takes one int parameter and returns void.
If we want to define a delegate that represent a function that takes to parameters and has a return parameter, we could do it like this:
| delegate double AnotherDelegateName(long firstParameter, long secondParameter); |
Also, other delegates types can be easily declared. For example to define a delegate that will represent a method with no parameters but with a return value, wemight write this:
delegate string DelegateName(); |
After we declare our delegate, we can create an instance of it so that we can use it to store details of a particular method.
Lets follow this simple delegate example to make sure we understand the way we must use them:
class MathsOperations { public static double MultiplyByTwo(double value) { return value*2; } public static double Square(double value) { return value*value; } } using System; namespace SimpleDelegate { delegate double DoubleOp(double x); class MainEntryPoint { static void Main() { DoubleOp [] operations = { new DoubleOp(MathsOperations.MultiplyByTwo), new DoubleOp(MathsOperations.Square) }; for (int i=0 ; i<operations.Length ; i++) { ProcessAndDisplayNumber(operations[i], 2.0); ProcessAndDisplayNumber(operations[i], 7.94); ProcessAndDisplayNumber(operations[i], 1.414); Console.WriteLine(); } } static void ProcessAndDisplayNumber(DoubleOp action, double value) { double result = action(value); Console.WriteLine("Value is {0}, result of operation is {1}", value, result); } |
As you will see, putting some delegates into an array is no problem at all. Each element of the array gets initialized to refer to a different operation.
We loop through the array, applying each operation to three different values.
Here, we are passing in the name of a delegate, but without any parameters. Given that operations[i] is a delegate, syntactically:
The ProcessAndDisplayNumber() method is defined to take a delegate as its first parameter: static void ProcessAndDisplayNumber(DoubleOp action, double value) Then, when in this method, we call: double result = action(value); This actually causes the method that is wrapped up by the action delegate instance to be called and its return result stored in Result. Running this sample gives the following: SimpleDelegate Using operations[0]: Value is 2, result of operation is 4 Value is 7.94, result of operation is 15.88 Value is 1.414, result of operation is 2.828 Using operations[1]: Value is 2, result of operation is 4 Value is 7.94, result of operation is 63.0436 Value is 1.414, result of operation is 1.999396
If we want to call more than one method, we need to make an explicit call through a delegate more than once. A delegate can wrap more than one method. This kind of delegates are called multicast delegates. When called, a multicast delegate will successively call each method in order.
A multicast delegate must return a void, for it to make sense. Actually, if the compiler sees a delegate that returns a void, it will automatically assume that you mean a multicast delegate.
Considering the code from the simple delegate exampl, here's how to modify it and make it use a multicast delegate.
delegate void DoubleOp(double value); // delegate double DoubleOp(double value); // can’t do this now class MainEntryPoint { static void Main() { DoubleOp operations = new DoubleOp(MathOperations.MultiplyByTwo); operations += new DoubleOp(MathOperations.Square); |
DoubleOp operation1 = new DoubleOp(MathOperations.MultiplyByTwo); DoubleOp operation2 = new DoubleOp(MathOperations.Square); DoubleOp operations = operation1 + operation2; |
We need to display the values, not return them:
| class MathOperations { public static void MultiplyByTwo(double value) { double result = value*2; Console.WriteLine( “Multiplying by 2: {0} gives {1}â€Â, value, result); } public static void Square(double value) { double result = value*value; Console.WriteLine(“Squaring: {0} gives {1}â€Â, value, result); } } |
| static void ProcessAndDisplayNumber(DoubleOp action, double value) { Console.WriteLine(“\nProcessAndDisplayNumber called with value = “ + value); action(value); } |
|
| static void Main() { DoubleOp operations = new DoubleOp(MathOperations.MultiplyByTwo); operations += new DoubleOp(MathOperations.Square); ProcessAndDisplayNumber(operations, 2.0); ProcessAndDisplayNumber(operations, 7.94); ProcessAndDisplayNumber(operations, 1.414); Console.WriteLine(); } |
Nobody posted any comments regarding this story. Be the first!
Discuss