Monday, November 22, 2010

CTS Delegate Types

Delegates are the .NET equivalent of a type-safe, C-style function pointer. The key difference is that a
.NET delegate is a class that derives from System.MulticastDelegate, rather than a simple pointer to a
raw memory address. In C#, delegates are declared using the delegate keyword.
// This C# delegate type can "point to" any method
// returning an int and taking two ints as input.
delegate int BinaryOp(int x, int y);Delegates are useful when you wish to provide a way for one entity to forward a call to another
entity and provide the foundation for the .NET event architecture. As you will see in Chapters 11 and 19,
delegates have intrinsic support for multicasting (i.e., forwarding a request to multiple recipients) and
asynchronous method invocations (i.e., invoking the method on a secondary thread).

No comments:

Post a Comment