Asynchronous Invocation
So far, you’ve seen delegates that return nearly instantaneously when they are being invoked. For example:
Func<int, int, int> add = (a, b) => a + b;int three = add(1, 2); // returns in nanoseconds
However, delegates can invoke long-running operations, too. If the method being referred to takes a long while to complete, the invocation site of the delegate will be blocked until the call returns. This is an important realization for APIs that accept as their input a delegate that gets called subsequently: The API can’t know how the call will perform given certain inputs. For example, in our calculator example, we could add the following custom function:
calc.AddOperator("F1", ...
Get C# 5.0 Unleashed now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.