11.5. Sending Object State Notifications using Delegates

Clearly, the previous SimpleDelegate example was intended to be purely illustrative in nature, given that there would be no compelling reason to define a delegate simply to add two numbers! To provide a more realistic use of delegate types, let's use delegates to define a Car class that has the ability to inform external entities about its current engine state. To do so, we will take the following steps:

  • Define a new delegate type that will send notifications to the caller.

  • Declare a member variable of this delegate in the Car class.

  • Create a helper function on the Car that allows the caller to specify the method to call back to.

  • Implement the Accelerate() method to invoke the delegate's ...

Get Pro C# 2010 and the .NET 4 Platform, Fifth 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.