Chapter 8

Delegating Those Important Events

IN THIS CHAPTER

Bullet Solving callback problems with delegates

Bullet Using delegates to customize a method

Bullet Using anonymous methods

Bullet Creating events in C#

This chapter looks into a corner of C# that has been around since the birth of the language. The ability to create a callback, a method used to handle events, is essential for C# applications of all sorts. In fact, the callback appears in applications of every kind today. Even web-based applications must have some sort of callback mechanism to allow them to work properly.

The alternative is to hold up the application while you wait for something to happen, which means that the application won’t respond to anything but the anticipated input. That’s how the console applications used in examples to this point work. The Console.Read() call essentially stops the application until the user does something. A console application can work in this manner, but when a user could click any button on a form, you must have something better — a callback mechanism. In C#, you implement a callback by using a delegate, ...

Get C# 10.0 All-in-One For Dummies 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.