0

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

Mou
  • 15,673
  • 43
  • 156
  • 275

2 Answers2

1

EventHandler actually has a very limited signature, two parameters where the first is always type object, and the second has to be a class type inheriting System.EventArgs. But considering System.Action<T, ...> and System.Func<T, ..., TReturn>, it is very rare that you need to declare another. In fact most of the original BCL delegate types MethodInvoker, Converter<T, U>, Predicate<T>, and even EventHandler<T> probably wouldn't have been defined if the Action and Func families had existed at that time.

Where you do still need to declare your own delegate is when you need by-reference parameters. The ref and out keyword cannot be passed through a generic type parameter, so if you want a delegate matching the TryParse family, you'd be unable to say Func<string, out T, bool> and would need a custom declaration

/* return true if parsing successful */
delegate bool TryParser<T>(string from, out T into);

It's very uncommon to use by-reference parameters with events, so you'll probably never have to declare a delegate for use as an event type.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
0

You need a custom delegate in cases where you want an event that receives parameters others than those specified by EventHandler

For example, assume your Animal class wants to raise an event each time another Animal tried to eat it, this event might need to send both the predator and the animal being attacked. With EventHandler you'll need to create a new class as a wrapper for both animals and send an instance of it as the event parameter, it is simpler with a delegate:

 public delegate void AttackedDelegate(Animal predator, Animal victim);
//...
public event AttackedDelegate OnAttacked;

Although with the introduction of the Action<> delegates I think it is a lot less common that you would need to declare your own for reasons other than readability. So the above example can be expressed as

public event Action<Animal, Animal> OnAttacked;
KMoussa
  • 1,568
  • 7
  • 11
  • i some what understand but u said "You need a custom delegate in cases where you want an event that receives parameters others than those specified by EventHandler" can u plzz post code which show when need to use custom delegate. thanks – Mou Nov 05 '16 at 22:11
  • @Mou edited with example, syntax not checked though but hope the idea is clear – KMoussa Nov 05 '16 at 22:41