Tải bản đầy đủ (.pdf) (1 trang)

Subscribing to an Event

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (6.62 KB, 1 trang )



Subscribing to an Event
Like delegates, events come ready-made with a += operator. You subscribe to an event
by using this += operator. In the automated factory, the software controlling each
machine can arrange for the shutdown methods to be called when the
MachineOverheating event is raised like this:
TemperatureMonitor tempMonitor = new TemperatureMonitor();
...
tempMonitor.MachineOverheating += delegate { folder.StopFolding(0) };
tempMonitor.MachineOverheating += welder.FinishWelding;
tempMonitor.MachineOverheating += painter.PaintOff;
Notice that the syntax is the same as for adding a method to a delegate. You can even
subscribe by using an anonymous method. When the tempMonitor.MachineOverheating
event runs, it will call all the subscribing methods and shut the machines down.



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×