Why use delegates?
I understand HOW to use delegates, but in what situations would you actually use them? The examples I see in the literature are all in situations where the code could just be written without ever using them.
Why use delegates?
I understand HOW to use delegates, but in what situations would you actually use them? The examples I see in the literature are all in situations where the code could just be written without ever using them.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Posted Mar 4, 2009, 12:01 AM
// This is defined publicly somewhere, maybe in a .dll
public delegate void FinishedDelegate(int numOfItemsProcessed);
class DataProcessor
{
private FinishedDelegate callbackDel;
public void SetCallback(FinishedDelegate callback)
{
this.callbackDel = callback;
}
public void Process()
{
int i;
for (i = 0; i < 10; i++)
{
// Do stuff...
}
// invoke the delegate
this.callbackDel.Invoke(i);
}
}
So now when you create an instance of the DataProcessor class you can simply pass the delegate (typesafe function pointer) to the classes SetCallback method and it will invoke the delegate when the Process method finishes. A whole example:
public class Processor
{
private DataProcesser dp = new DataProcessor();
public MyClass()
{
this.dp.SetCallback(new FinishedDelegate(FinishedProcessing));
}
public void BeginProcessing()
{
this.dp.Process();
}
protected void FinishedProcessing(int numOfItemsProcessed)
{
// Processing finished, do whatever...
}
}
You see? Its basically like passing a tyepsafe function pointer to a method that may not ordinarily be accessible by whatever class needs it. You just roll up the pointer in a delegate and pass it to the method that wants it. This works very well when you have a method that needs to call another method when something occurs or something is finished, it doesnt care what the method does, or what the methods name is, just so long as the method has the proper signature.
Look at the EventHandler class, when you create a new instance of an EventHandler, it only accepts the address of a method that follows a certain signature. It doesnt care what the method does or what its name is, it only cares if the method returns void and takes an object and an EventArgs instance as arguments. This is the power of delegates.
I guess to sum it all up, you would use delegates in two instances (at least normally this is where I would use them):
1. When accessing the UI from a separate thread.
2. When you need to give a class a function to call, but the call needs to be generic enough to where the body of the function doesnt matter, just the signature of the function. Such as an AsyncCallback class instance (yet another good use of a delegate). The class isnt going to care what the function does, it will just call it when it needs to by using the delegate provided.
Bechir BejaouiPosted Mar 1, 2009, 6:49 PM
extend a classA in a classB
//Source object
class ClassA
{
//Deleagte declaration
public MyDelegate myDelegate;
}
//Destination object
class ClassB
{
ClassA classA;
public ClassB()
{
// ClassA initailization
classA = new ClassA();
//Extend the classA
classA.myDelegate += new MyDelegate(Method);
//Call the method
classA.myDelegate();
}
public void Method()
{
Console.WriteLine("Bonjour");
}
}
Events are based on Delegetes
class CustomEventArgs : EventArgs
{
public string Message{get; set;}
public CustomEventArgs(string Message)
{
this.Message = Message;
}
}
class Trigger
{
public delegate void EventHandler(Object sender, CustomEventArgs ce);
public event EventHandler TriggerEvent;
public void OnTriggerEvent(string Message)
{
TriggerEvent(this, new CustomEventArgs(Message));
}
}
To consume this event:
class Program
{
static void Main(string[] args)
{
Trigger trigger = new Trigger();
trigger.TriggerEvent+=new Trigger.EventHandler(trigger_TriggerEvent);
trigger.OnTriggerEvent("Hello");
trigger.OnTriggerEvent("How are you!! ; )");
Console.Read();
}
static void trigger_TriggerEvent(object sender, CustomEventArgs ce)
{
Console.WriteLine("Event is triggered and the message is {0}", ce.Message);
}
}
Delegate are used whithin Threading context as Callback operators those call methods executed within given threads' contexts
System.Threading.ThreadStart threadStart;
Thread myThread;
void Main
{
threadStart = new ThreadStart(Method);
myThread = new Thread(threadStart);
myThread.Start();
}
static void Method()
{
Console.WriteLine("Thread is lunched");
}
And so many other cases
Jan MontanoPosted Feb 26, 2009, 7:36 PM