Delegates and Events

The next chapter pertains to delegates and events, which are related topics. Function pointers are prevalent in unmanaged programming languages and helpful in streamlining algorithms and choosing functions at run time rather than statically at compile time. As function pointers, functions can be treated as local variables, function parameters, or even return values—which adds considerable flexibility in the way they are managed and called. Because pointers are simply pointers into raw memory, function pointers are not safe. .NET offers a type-safe version of function pointers called delegates. Delegates provide the same flexibility as function pointers but in a type-safe manner.

Delegates often are used as callbacks, where functions are called in response to an event or on demand. The Event model promotes an effective paradigm for callbacks, where there are publishers and subscribers. Publishers expose something that happens—better known as an event. One or more subscribers subscribe to that event with a delegate. The publisher then calls all the functions when the event occurs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.202.240