Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Lưu ý rằng trong việc hình thành chuỗi, các loại System.Double đã sử dụng CurrencyDecimalSeparator, CurrencyDecimalDigits, và tính chất CurrencySymbol của thể hiện NumberFormatInfo trở về từ phương pháp CultureInfo.GetFormat. | CHAPTER 12 Delegates and Events D elegates provide a built-in language-supported mechanism for defining and executing callbacks. Their flexibility allows you to define the exact signature of the callback and that information becomes part of the delegate type itself. Anonymous functions are forms of delegates that allow you to shortcut some of the delegate syntax that in many cases may be overkill. Building on top of delegates is the support for events in Visual Basic VB and the .NET platform. Events provide a uniform pattern for hooking up callback implementations and possibly multiple instances thereof to the code that triggers the callback. Overview of Delegates The common language runtime CLR provides a runtime that supports a flexible callback mechanism and delegates are the preferred method of implementing these callbacks. When you declare a delegate in your code the VB compiler generates a class derived from MulticastDelegate and the CLR implements all of the interesting methods of the delegate dynamically at run time. The delegate contains a couple of useful fields. The first one holds a reference to an object and the second holds a method pointer. When you invoke the delegate the instance method is called on the contained reference. However if the object reference is Nothing the runtime understands this to mean that the method is a shared method. One delegate type can handle callbacks to either an instance or shared method. Moreover invoking a delegate is exactly the same syntactically as calling a regular function. Therefore delegates are perfect for implementing callbacks. As you can see delegates provide an excellent mechanism to decouple the method being called on an instance from the actual caller. In fact the caller of the delegate has no idea or necessity to know if it is calling an instance method or a shared method or on what exact instance it is calling. To the caller it is calling arbitrary code. The caller can obtain the delegate instance .