Delegates
A delegate is a type
that represents references to methods with a particular parameter
list and return type.When you instantiate a delegate, you can
associate its instance with any method with a compatible signature
and return type. You can invoke (or call) the method through the
delegate instance.
When
and Why to use Delegates
A delegate can be seen as
a placeholder for a/some method(s).
By defining a delegate,
you are saying to the user of your class "Please feel free to
put any method that match this signature here and it will be called
each time my delegate is called".
Typical use is of course
events. All the OnEventX delegate to the methods the user defines.
Delegates are useful to
offer to the user of your objects some ability to customize their
behaviour. Most of the time, you can use other ways to achieve the
same purpose and I do not believe you can ever be forced to create
delegates. It is just the easiest way in some situations to get the
thing done.
Comments
Post a Comment