//---assign the delegate to point to AddTwoNumbers()---
MethodDelegate del = AddTwoNumbers;
Next, define a delegate of type AsyncCallback
:
//---creates a AsyncCallback delegate---
AsyncCallback callback = new AsyncCallback(ResultCallback);
The AsyncCallback
is a delegate that references a method to be called when an asynchronous operation completes. Here, you set it to point to ResultCallback
(which you will define later).
To call the AddTwoNumbers()
methods asynchronously, you use the BeginInvoke ()
method of the del
delegate, passing it two integer values (needed by the AddTwoNumbers()
method), as well as a delegate to call back when the method finishes executing:
//---invoke the method asynchronously---
Console.WriteLine('Invoking the method asynchronously...');
IAsyncResult result = del.BeginInvoke(5, 3, callback, null);
Console.WriteLine('Continuing with the execution...');
The BeginInvoke()
method calls the delegate asynchronously, and the next statement continues execution after the async delegate is called. This method returns a variable of type IAsyncResult
to represent the status of an asynchronous operation.
To obtain the result of the calculation, you define the ResultCallback()
method, which takes in an argument of type IAsyncResult
:
static private void ResultCallback(IAsyncResult ar) {
MethodDelegate del =
(MethodDelegate)((AsyncResult)ar).AsyncDelegate;
//---get the result---
int result = del.EndInvoke(ar);
Console.WriteLine('Result of addition is: ' + result);
}
Within the ResultCallback()
method, you first obtain the delegate to the AddTwoNumbers()
method by using the AsyncDelegate
property, which returns the delegate on which the asynchronous call was invoked. You then obtain the result of the asynchronous call by using the EndInvoke()
method, passing it the IAsyncResult
variable (ar
).
Finally, to demonstrate the asynchronous calling of the AddTwoNumbers()
method, you can insert a Sleep()
statement to delay the execution (simulating long execution):
static private int AddTwoNumbers(int num1, int num2) {
return num1 + num2;
}
Figure 7-3 shows the output of this program.

Figure 7-3
When using asynchronous callbacks, you can make your program much more responsive by executing different parts of the program in different threads.
Chapter 10 discusses more about threading.
Anonymous Methods and Lambda Expressions
Beginning with C# 2.0, you can use a feature known as
An anonymous method is an 'inline' statement or expression that can be used as a delegate parameter. To see how it works, take a look at the following example:
class Program {
delegate void MethodsDelegate(string Message);
static void Main(string[] args) {
MethodsDelegate method = Method1;
//---call the delegated method---
method('Using delegate.');
Console.ReadLine();
}
static private void Method1(string Message) {
Console.WriteLine(Message);
}
}
Instead of defining a separate method and then using a delegate variable to point to it, you can shorten the code using an anonymous method:
class Program {
delegate void MethodsDelegate(string Message);
static void Main(string[] args) {
//---call the delegated method---
Console.ReadLine();
}
}
In this expression, the method
delegate is an anonymous method:
Anonymous methods eliminate the need to define a separate method when using delegates. This is useful if your delegated method contains a few simple statements and is not used by other code because you reduce the coding overhead in instantiating delegates by not having to create a separate method.
In C# 3.0, anonymous methods can be further shortened using a new feature known as
The preceding code using anonymous methods can be rewritten using a lambda expression:
class Program {
delegate void MethodsDelegate(string Message);
static void Main(string[] args) {
//---call the delegated method---