11.4. The Simplest Possible Delegate Example

To be sure, delegates can cause a great deal of confusion when encountered for the first time. Thus, to get the ball rolling, let's take a look at a very simple Console Application program (named SimpleDelegate) that makes use of the BinaryOp delegate type you've seen previously. Here is the complete code, with analysis to follow:

namespace SimpleDelegate { // This delegate can point to any method, // taking two integers and returning an integer. public delegate int BinaryOp(int x, int y); // This class contains methods BinaryOp will // point to. public class SimpleMath { public static int Add(int x, int y) { return x + y; } public static int Subtract(int x, int y) { return x - y; } } class Program ...

Get Pro C# 2010 and the .NET 4 Platform, Fifth Edition 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.