Delegates

Once upon a time, in a strange land south of here, lived Peter. Peter was a diligent worker who would readily accept requests from his boss. However, his boss was a mean, untrusting man who insisted on steady progress reports. Because Peter did not want his boss standing in his office looking over his shoulder, Peter promised to notify his boss whenever his work progressed. Peter implemented this promise by periodically calling his boss back via a typed reference like so:

class Worker {
  public void Advise(Boss boss) {this.boss = boss; }
  public void DoWork() {
    Console.WriteLine("Worker: work started");
    if( boss != null ) boss.WorkStarted();

    Console.WriteLine("Worker: work progressing");
    if( boss != null ) boss.WorkProgressing(); Console.WriteLine("Worker: ...

Get Windows Forms Programming in C# 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.