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.

FIGURE 17.10 Invoking ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.