Deleagtes could be used in several senarios
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
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