CHAPTER 23

image

Events

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 O’Reilly online learning.

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