What is the differences between these?
- public event EventHandler eEH;
- public EventHandler EH;
They are very similar, but different:
- The first one is an event, the second one is a public field of delegate type.
- No one can assign a delegate to your event, they only can add (+=) or remove delegates (-=), but in addition to add (+=), and remove (-=), they can assign (=) delegate to the public field.
- No one can raise event like a delegate outside of your class, but they can invoke your public delegate field.
- Like properties and methods, you can define events in interfaces but you cannot do the same for fields.
Example - Difference between an event and a delegate field
Here are a few examples to demonstrate the differences which I mentioned above:
-
class1.MyEvent = Something;
doesn't work. -
class1.MyDelegateField = Something
works and will assign a new multicast delegate to the field. -
class1.MyEvent();
doesn't work outside of your class. -
class1.MyDelegateField();
works and will run all the delegates which has been assigned to the field. -
interface ISomething { event EventHandler MyEvent;}
is a valid declaration. -
interface ISomething { EventHandler MyDelegateField}
is invalid.
More information
You can learn more about the events: