Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Nbsp;   Generic Property Accessor Methods

Because properties are really just methods, and because C# and the CLR allow methods to be generic, sometimes people want to define properties that introduce their own generic type parameters (as opposed to using the enclosing type’s generic type parameter). However, C# does not allow this. The main reason why properties cannot introduce their own generic type parameters is because they don’t make sense conceptually. A property is supposed to represent a characteristic of an object that can be queried or set. Introducing a generic type parameter would mean that the behavior of the querying/setting could be changed, but conceptually, a property is not supposed to have behavior. If you want your object to expose some behavior—generic or not—define a method, not a property.


C HA P T E R 1 1

Events

In this chapter:

Designing a Type That Exposes an Event.................................................. 250

How the Compiler Implements an Event................................................. 256

Designing a Type That Listens for an Event............................................... 258

Explicitly Implementing an Event............................................................ 260

 

In this chapter, I’ll talk about the last kind of member a type can define: events. A type that defines an event member allows the type (or instances of the type) to notify other objects that something special has happened. For example, the Button class offers an event called Click. When a Button object is clicked, one or more objects in an application may want to receive notification about this event in or- der to perform some action. Events are type members that allow this interaction. Specifically, defining an event member means that a type is offering the following capabilities:

■ A method can register its interest in the event.

 

■ A method can unregister its interest in the event.

 

■ Registered methods will be notified when the event occurs.

 

Types can offer this functionality when defining an event because they maintain a list of the regis- tered methods. When the event occurs, the type notifies all of the registered methods in the collection.

The common language runtime’s (CLR’s) event model is based on delegates. A delegate is a type- safe way to invoke a callback method. Callback methods are the means by which objects receive the notifications they subscribed to. In this chapter, I’ll be using delegates, but I won’t fully explain all their details until Chapter 17, “Delegates.”

To help you fully understand the way events work within the CLR, I’ll start with a scenario in which events are useful. Suppose you want to design an email application. When an email message arrives, the user might like the message to be forwarded to a fax machine or a pager. In architecting this ap- plication, let’s say that you’ll first design a type called MailManager that receives the incoming email messages. MailManager will expose an event called NewMail. Other types (such as Fax and Pager) may register interest in this event. When MailManager receives a new email message, it will raise the event, causing the message to be distributed to each of the registered objects. Each object can process the message in any way it desires.




When the application initializes, let’s instantiate just one MailManager instance—the application can then instantiate any number of Fax and Pager types. Figure 11-1 shows how the application initializes and what happens when a new email message arrives.

 
 

 

FIGURE 11-1Architecting an application to use events.

 

Here’s how the application illustrated in Figure 11-1 works: the application initializes by construct- ing an instance of MailManager. MailManager offers a NewMail event. When the Fax and Pager objects are constructed, they register an instance method with MailManager’s NewMail event so that MailManager knows to notify the Fax and Pager objects when new email messages arrive. Now, when MailManager receives a new email message (sometime in the future), it will raise the NewMail event, giving all of the registered methods an opportunity to process the new message in any way they want.

 

 


Date: 2016-03-03; view: 656


<== previous page | next page ==>
Nbsp;   Parameterful Properties | Nbsp;   Designing a Type That Exposes an Event
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)