In C#, delegates represent methods that are callable without knowledge of the target object. Delegates enable scenarios that some other languages have addressed with function pointers. However, unlike function pointers, delegates are object-oriented and type-safe.
A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.
An interesting and useful property of a delegate is that it does not know or care about the class of the object that it references. Any object will do; all that matters is that the method's argument types and return type match the delegate's. This makes delegates perfectly suited for "anonymous" invocation.
The signature of a single cast delegate is shown below:
delegate result-type identifier ([parameters]);
where:
- result-type: The result type, which matches the return type of the function.
- identifier: The delegate name.
- parameters: The Parameters, that the function takes.
Examples:
public delegate void SimpleDelegate ()
no parameters and returns no value.
public delegate int ButtonClickHandler (object obj1, object obj2)
two objects as parameters and returns an int.
A delegate will allow us to specify what the function we'll be calling looks like without having to specify which function to call. The declaration for a delegate looks just like the declaration for a function, except that in this case, we're declaring the signature of functions that this delegate can reference.
There are three steps in defining and using delegates: declaration, instantiation, and invocation.
Delegate Declaration:
public class DeligateClass
{
// Declare a delegate
public delegate void MessageHandler(string message);
Function Class:
This class contains the functions will be called by delegates
public class FunctionClass
{
{
public static void AlertMessage(string s)
public static void ConsoleMessage(string s)
Instantiation and Invocation:
Client Application to use the defined Delegate
Simple Delegate
Create a windows form and add one button to it and paste the following code on its click event.
private void button1_Click(object sender, System.EventArgs e)
{
//Instantiating a delegate
DeligateClass dc = new DeligateClass();
DeligateClass.MessageHandler ll = null;
ll += new DeligateClass.MessageHandler(FunctionClass.AlertMessage);
//Calling a delegate
dc.Process(ll);
}
When you will click on button1, it will show 'Begin Message' and 'End Message' message box.
Multicast Delegate
Being able to point to member functions is nice, but there are more tricks you can do with delegates. In C#, delegates are multicast, which means that they can point to more than one function at a time (that is, they're based off the System.MulticastDelegate type). A multicast delegate maintains a list of functions that will all be called when the delegate is invoked.
Add one more button to form with following code.
private void button2_Click(object sender, System.EventArgs e)
{
DeligateClass dc = new DeligateClass();
DeligateClass.MessageHandler ll = null;
ll += new DeligateClass.MessageHandler(FunctionClass.AlertMessage);
ll += new
DeligateClass.MessageHandler(FunctionClass.ConsoleMessage);
dc.Process(ll);
}
When you will click on button2, in addition to 'Begin Message' and 'End Message' message box, it will also write to console of visual studio (VS).
Delegates and interfaces are similar in that they enable the separation of specification and implementation. Multiple independent authors can produce implementations that are compatible with an interface specification. Similarly, a delegate specifies the signature of a method, and authors can write methods that are compatible with the delegate specification.
There are direction defined by Microsoft when to use delegate and when to use interface.
Delegates are ideally suited for use as events - notifications from one component to "listeners" about changes in that component.
Regards,
Paresh Bhole
No comments:
Post a Comment