What is the difference between delegate type and event handler type?

Three related idioms: event, delegate, event handler. I am always embarrassed by those who are β€œadded” to whom.

event += handler event += delegate handler += delegate 

From what I know:

  • delegate: pointer to a function with a known signature.
  • event-handler: delegate registered for the event. Basically, is it the same as a delegate?
  • event: a list of delegates \ event-handlers that are executed when an event is raised using an event ()

What confuses me more about this signature on MSDN:

 public delegate void EventHandler(Object sender, EventArgs e) 
+6
source share
3 answers

Here is my resume (please correct me if I am wrong):

  • delegate is a pointer to a method (instance \ static)

  • eventHandler is a delegate with a specific signature (sender, eventArgs)

  • event is an abstraction of access to a delegate of any type, but usually an eventHandler by convention

     //We declare delegates as a new type outside of any class scope (can be also inside?) public delegate retType TypeName (params...) //Here we assign public TypeName concreteDeleagteName = new TypeName (specificMethodName); //Declaring event //a. taken from http://stackoverflow.com/questions/2923952/explicit-event-add-remove-misunderstood private EventHandler _explicitEvent; public event EventHandler ExplicitEvent { add { if (_explicitEvent == null) timer.Start(); _explicitEvent += value; } remove { _explicitEvent -= value; if (_explicitEvent == null) timer.Stop(); } } //or: b.auto event - the compiler creates a "hidden" delegate which is bounded to this event public event TypeName eventName; 

I want to recommend a great article Event Handling in .NET Using C # .

Thus, we can only attach (eventName):

 eventName += new TypeName (specificMethodName); 

Which is equivalent (_eventName is a delegate of \ eventHandler):

 _eventName += new TypeName (specificMethodName); 
-1
source

A delegate added to the event, which "points" to the handler.

Thus, basically, when an event is created, the delegate collection it has will be called, which will result in invoking handlers associated with these delegates.

 //declare delegate public delegate void EventHandler( Object sender, EventArgs e) //create event based on delegate public event EventHandler evHandler; //function to attach to handler public static void Handler(object sender, EventArgs e) {} attach eventhandler function through delegate to event. event += new EventHandler(Handler); 
+3
source

An "event" is just a shortcut to two methods that work with a delegate - add and remove accessors . By default, the compiler makes a delegate for the event (unless you write your own accessories).

When you call someEvent += aDelegate; , you raise the add accessor event. This is usually converted by the compiler to a delegate += call for a delegate with the same signature as the event, similar to how automatic properties are automatically displayed in the support field. This is why the event looks like a delegate.

what this signature in MSDN confuses me more: public delegate void EventHandler (object sender, EventArgs e)

This signature is just the signature of the delegate. An event can, technically, use any delegate. However, by convention, it will always take two parameters: the first is the "sender" that raised the event, the second is the class that comes from EventArgs (for example, EventHandler and EventHandler<T> ).

+3
source

Source: https://habr.com/ru/post/896795/


All Articles