tailieunhanh - Delegates and Events
Classes are reference types that allow you to create instances of objects and use them in special ways to meet your application’s requirement. Another reference type in C# is delegate. Delegates allow you to change the reference to a method at runtime. This means that you can decide the execution of a method at run-time, based on the requirements of your application. The method can be activated at the occurrence of an event, where an event is associated with the delegate to call the method at run-time. . | Delegates and Events Chapter 12 Classes are reference types that allow you to create instances of objects and use them in special ways to meet your application s requirement. Another reference type in C is delegate. Delegates allow you to change the reference to a method at runtime. This means that you can decide the execution of a method at run-time based on the requirements of your application. The method can be activated at the occurrence of an event where an event is associated with the delegate to call the method at run-time. This chapter discusses the creation of delegates. This chapter also discusses how to implement multicast delegates. In addition it discusses how you can use events with delegates. Objectives In this chapter you will learn to fl Implement delegates fl Implement multicast delegates fl Use events with delegates Introducing Delegates Delegates in C allow you to dynamically change the reference to the methods in a class. Consider an example of a coffee vending machine which dispenses different flavors of coffee such as cappuccino and black coffee. On selecting the desired flavor of coffee the vending machine decides to dispense the ingredients such as milk powder coffee powder hot water cappuccino coffee powder. All the materials are placed in different containers inside the vending machine. The required material is dispensed when you select a flavor. Suppose you select black coffee the vending machine will call methods to dispense hot water and coffee powder only. The reference to these methods is made dynamically when you press the required button to dispense black coffee. A delegate is a reference type variable which holds the reference to a method. This reference can be changed at runtime as desired. Although delegates are a general-purpose mechanism for indirectly calling methods at runtime their primary use in C programming is for implementing events and the call-back methods. To implement delegates in your application you need to .
đang nạp các trang xem trước