all we know delegate is function pointer. so when we work with event then we declare delegate but i like to know in which kind of situation we have to declare delegate when working with events like public delegate void OnButtonClickDelegate();
here i am refering a code sample which show we can use in built delegate EventHandler
instead of declare delegate explicitly.
public class ArgsSpecial : EventArgs
{
public ArgsSpecial (string val)
{
Operation=val;
}
public string Operation {get; set;}
}
public class Animal
{
// Empty delegate. In this way you are sure that value is always != null
// because no one outside of the class can change it.
public event EventHandler<ArgsSpecial> Run = delegate{}
public void RaiseEvent()
{
Run(this, new ArgsSpecial("Run faster"));
}
}
Animale animal= new Animal();
animal.Run += (sender, e) => Console.WriteLine("I'm running. My value is {0}", e.Operation);
animal.RaiseEvent();
so when EventHandler
in built delegate solve my purpose then we no need to declare delegate explicitly when working with events. so tell me where we have to declare delegate explicitly and EventHandler
may not solve our purpose. thanks