Chapter 12. Delegates and Events
When a head of state dies, the president of the United States typically does not have time to attend the funeral personally. Instead, he dispatches a delegate. Often this delegate is the vice president, but sometimes the VP is unavailable and the president must send someone else, such as the secretary of state or even the first lady. He doesn’t want to “hardwire” his delegated authority to a single person; he might delegate this responsibility to anyone who is able to execute the correct international protocol.
The president defines in advance what authority will be delegated (attend the funeral), what parameters will be passed (condolences, kind words), and what value he hopes to get back (good will). He then assigns a particular person to that delegated responsibility at “runtime” as the course of his presidency progresses.
In programming, you are often faced with situations where you need to
execute a particular action, but you don’t know in
advance which method, or even which object, you’ll
want to call upon to execute that action. For example, a button might
know that it must notify some object when it is
pushed, but it might not know which object or objects need to be
notified. Rather than wiring the button to a particular object, you
will connect the button to a delegate
and then resolve that delegate to a particular method when the
program executes.
In the early, dark, and primitive days of computing, a program would begin execution and ...
Get Programming C#, Second 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.