Basic Introduction
In simple terms, Events are special objects in
.NET which allows programmer to give notification to the external world. Or we
can also say that it is a means of communication between the object to the
external world. Actually it maps to an existing delegate that acts as a Type of
it which eventually points to a method. So, when the event is subscribed, it
needs a method which the object will call at certain time when event is raised
in the system. Event is the structured way of calling delegate into the class.
Internal Working
Events in .NET is ThreadSafe and uses
Multicast Delegates internally. It is provided with .NET to give a standard way
of notifying the external world rather than dealing with delegates directly. If
we create a class for the event handling purpose it will have mainly 3 things:
- Private event of type EventHandler.
- Public event accessor which gives an
interface for add and remove operation for the event.
- Public virtual method which can be called
from code when programmer want to raise the event.
Thus the actual event variable is not exposed
outside.
Delegates internally calls all its methods configured in its invocation list,
the calls will be automatically managed. Each MulticastDelegate holds an array
of MethodInfo internally which is upcast as objects