In order to better understand wanted to know what is different Event
from Delegate
And also the use of operators + = and – = for methods in C #
Answer 1, Authority 100%
Let’s start with the fact that Event
and Delegate
are two different things, as different as a car and carrot. The difference between the field and event is about the same as between the field and the property: Event sometimes looks like a delegate field. Let’s deal with it.
delegate
is Class containing a “pattern” method, that is, the method signature. Variable delegate type – object type MulticastDelegate
(more precisely, derived from it), which can contain one or more objects, which are a method with a compatible signature compatible with a “pattern” (counter and covariance slightly complicates a picture). That is, it is a variable that can contain functions. For such variables, the +
operation is defined, which combines the components of the functions into one new function, and the symmetrical operation . These operations automatically generate derivatives
+ =
and - =
.
event
is just pair of methods in the class, denoted as add
and remove
and having arbitrary Semantics selected by the programmer. (Analogue – Getter and Setter Properties.) In default implementation for Event, a hidden field of delegate type is started, and Add / remove is added or removed from it methods (under LOCK). (To confuse the picture slightly, this is a hidden field available by the same name as the event
.) Add
/ remove
Event
, Are called , respectively, as + =
and - =
. No operations +
/ -
, of course, no.
Let’s still run in distinction of Event in the classroom from the public fields delegate type.
Consider the case when EVENT is implemented “by default”, that is, with an implicit delegate field. The difference is that:
-
For a delegate field, you Full access to it. You can – also outside the class! – Disassemble
MulticastDelegate
to pieces and collect new, you can replace it with your own or assign itnull
you can call it, you can copy it to yourself in a variable. You have full access, as well as any public field. (This is, of course, blatantly violates encapsulation.)For Event, you can only write
Instance.Event + = Handler
andInstance.Event - = Handler
, which is displayed on thefunction
andRemove
, which in turn again cause+ =
and- =
for an automatically implemented delegate. You do not have any other access. Inside the class you, however, you can read the value of this delegate using the Event name. (This is necessary, for example, in order to cause this delegate; VB, unlike C #, has a specialRaiseEvent
method with the desired checks.) -
Another subtle difference (already mentioned in the comments to other answers) is that
add
/remove
are called under Lock, which makes them Thread-safe. However, the Event’s thread safety is very complex (almost said “impossible in principle”) (see article [2]), so that this difference with proper programming is insignificant: Event operations are usually must Take only in one stream. Note that the Lock is not available outside theAdd
/remove
so that you cannot copy delegate to the local variable under it for a thread-safe call *:void raise () { EventHandler Copy; lock (& lt; sorry, not available & gt;) Copy = Myevent; If (Copy! = NULL) Copy (...); }
however, in modern versions .NET instead of
Lock
usedIntelocked
– and callMYEVENT? .INVOKE (...)
thread-safe .
For the case when EVENT is not implemented by “Default”, there is practically nothing in common. Methods Add
and Remove
can do anything:
int subscribercount = 0;
Public Event Eventhandler Myevent
{
Add {Subscribercount ++; }
Remove {subscribercount--; }
}
Programmer can to start a delegate independently and “fold” signed handlers there, but this is the principle of his kind will. On the other hand, it is still recommended not to break the semantics of the class expected clients, and use Event for destination.
Literature:
In addition, the delegate
keyword is used to declare anonymous function (older parallel syntax for lambda expressions ).
* In modern versions of C #, the Myevent? .invoke (...)
is recommended for a thread-safe call? However, with thread safety in events, everything is not smooth, as explained in the article [2].
Answer 2, Authority 50%
The main difference between Event from Delegate is that EVENT can only be run in the class in which it is declared.
In addition, if there is an event, the compiler creates not only the appropriate private field-delegate, but also two open methods for subscription and its cancellation on the events.
From the smaller differences, it is worth remembering that the event, unlike the delegate, can be a member of the interface. This is because delegates are always fields, while interfaces contain fields cannot.
In addition, an event, unlike delegate, cannot be a local variable in the method.
In general, about events and their relationships with delegates, read by Richter, chapter 11, which is so called “Events”