Delegate Instances

Although delegate types provide a type-safe way to refer to methods, instances concretize this notion by referring to a particular method on some object (or using null for a static method). In our running example, we give a first example of instantiating a delegate instance:

static void Main() {    BinOp add = new BinOp(Add);    int three = add(1, 2); // calls the Add method through the delegate}

This is one possible way to create a delegate instance that has been supported since the early C# 1.0 days. As shown in Figure 17.6, Visual Studio hints at the required method signature for the delegate target.

FIGURE 17.6 IntelliSense for instantiation of a delegate type.

Since C# 2.0, the syntax for ...

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.