What are delegates?

Delegates are basically function pointers. A delegate is a class that can hold a reference to a method. Unlike other classes, a delegate class has a signature, and it can hold references only to methods that match its signature. A delegate is thus equivalent to a type-safe function pointer or a callback. A Delegate is Passing a method as an Argument to a Existing method to create an Event eg:

Public delegate btn_click(Object sender, clickevent e);
public event btn_click BTNClick;

Delegate is kinda like a pointer to a function in C++ or like an event handler in Java.
You can use it to “multicast” which means running multiple functions in different instances of object already created.
This is useful when you want your objects to “register” to an event raised by another object.
The way it works is the object you are registered to listen to receives the delegate of the function it is supposed to run in your object, the delegate is then run from it. (if you switch the word delegate for pointer, this would be much simpler).

Tagged , . Bookmark the permalink.

Leave a Reply