S
Smithers
I'm wanting to make sure I have an accurate understanding of the
relationship between delegates and events in .NET.
By "events in .NET" I'm referring to specifically to events defined on the
'event' keyword - not other ways to do events, like old-school callbacks.
Please consider the following event declaration:
line 1: public delegate void SomethingJustHappened(string whatHappened);
line 2: public event SomethingJustHappened MyEvent;
Are the following statements accurate? If any are not, I would appreciate it
if you would briefly explain why it is [they are] inaccurate.
1. line 1 above creates a new type. That type is an extension of
System.Delegate. So, SomethingJustHappened is a System.Delegate.
2. The "data type of the event" is SomethingJustHappened (our delegate type
from line 1). [is that how we'd say it - "data type of an event"???]
3. Because the event declared in line 2, MyEvent, IS-A
SomethingJustHappened, MyEvent IS-[also]-A System.Delegate. In other words,
event == delegate. Yes?
4.When we "register a method with an event" we are really registering the
method with the delegate upon which the event is based (or the delegate that
IS ALSO the event).
5. Conclusion #1 - an event (defined on the 'event keyword') is really a
wrapper for a delegate --- or the event is just *another name* by which we
can refer to the delegate.
6. Conclusion #2 - The conceptualization of "event" is little more than a
conceptual convenience for us to think about a specialized use of delegates.
In face, we can substitute delegates in any place where we have an event.
I appreciate your time and consideration on this. I'm wanting to understand
what's going on and not "just make it work."
-S
relationship between delegates and events in .NET.
By "events in .NET" I'm referring to specifically to events defined on the
'event' keyword - not other ways to do events, like old-school callbacks.
Please consider the following event declaration:
line 1: public delegate void SomethingJustHappened(string whatHappened);
line 2: public event SomethingJustHappened MyEvent;
Are the following statements accurate? If any are not, I would appreciate it
if you would briefly explain why it is [they are] inaccurate.
1. line 1 above creates a new type. That type is an extension of
System.Delegate. So, SomethingJustHappened is a System.Delegate.
2. The "data type of the event" is SomethingJustHappened (our delegate type
from line 1). [is that how we'd say it - "data type of an event"???]
3. Because the event declared in line 2, MyEvent, IS-A
SomethingJustHappened, MyEvent IS-[also]-A System.Delegate. In other words,
event == delegate. Yes?
4.When we "register a method with an event" we are really registering the
method with the delegate upon which the event is based (or the delegate that
IS ALSO the event).
5. Conclusion #1 - an event (defined on the 'event keyword') is really a
wrapper for a delegate --- or the event is just *another name* by which we
can refer to the delegate.
6. Conclusion #2 - The conceptualization of "event" is little more than a
conceptual convenience for us to think about a specialized use of delegates.
In face, we can substitute delegates in any place where we have an event.
I appreciate your time and consideration on this. I'm wanting to understand
what's going on and not "just make it work."
-S