Invoking Delegates
Creating delegate instances to refer to particular methods is a necessary step toward invoking those methods through the delegate. In passing, we’ve shown the way to do such invocations repeatedly: They just look like ordinary method calls. For example:
Func<int, int, int> add = (a, b) => a + b;int three = add(1, 2);
IntelliSense in Visual Studio will obviously give the caller a hand by showing what arguments are expected. Notice that names for those arguments come from the delegate type that’s being used. In the preceding example, we’re using one of the generic Func
types, where arguments have names like arg1
and arg2
. How this looks in Visual Studio is shown in Figure 17.10.
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.