Three related idioms: event, delegate, event-handler. I always get confused by who is "added" to who.
event += handler
event += delegate
handler += delegate
开发者_运维百科
From what I know:
- delegate: a pointer to a function with a known signature.
- event-handler: a delegate which is registered to an event. Basically, is it the same as a delegate?
- event: a list of delegates\event-handlers which are executed when the event is invoked using event()
What confuses me more is this signature in MSDN:
public delegate void EventHandler(Object sender, EventArgs e)
An "event" is really just shortcut for two methods that work with a delegate - the add and remove accessors. The compiler, by default, makes a delegate behind the event (if you don't write your own accessors).
When you call someEvent += aDelegate;
, you're calling the event's add
accessor. Normally, this is translated by the compiler into a delegate +=
call for a delegate with the same signature as the event - similar to how automatic properties automatically map to a backing field. This is why an event seems so similar to a delegate.
what confuses me more is this signature in MSDN: public delegate void EventHandler( Object sender, EventArgs e)
This signature is just a delegate signature. 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 a class that derives from EventArgs
(like EventHandler
and EventHandler<T>
).
The event has a delegate
added to it which "points" to a handler.
So basically, when the event
is raised, the collection of delegates it has, will be invoked, which as result will invoke handlers connected to those 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);
Here is my summary (please correct me if I'm 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 accessing a delegate of any type, but it's usually aneventHandler
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 the great article Event Handling in .NET Using C#.
So we can only attach (eventName):
eventName += new TypeName (specificMethodName);
Which is equivalent to (_eventName is a delegate\eventHandler):
_eventName += new TypeName (specificMethodName);
精彩评论