Chapter 22 discussed how delegates can be used to pass a reference to a method so that it can be called in a general way. Being able to call a method in such a way is useful in graphical user interfaces, such as the one provided by the classes in System.Windows.Controls. It’s fairly easy to build such a framework by using delegates, but there are significant limitations to the delegate approach. Events remove those limitations.

A Simple Example Using Delegates

The following is a simple example using delegates:

using System;public class Button{    public delegate void ClickHandler(object sender, EventArgs e);    public ClickHandler ...

Get A Programmer's Guide to C# 5.0, 4th 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.