1

C# has events built in with a keyword. So if you want to declare an event, you write:

public event MyEventHandler SomethingHappened;

It is convenient, as it does allow you to add multiple event handlers, although it also prevents you from customising how events are handled, such as if you want to ensure that certain event handlers would always run in a certain order.

It seems that code would be more flexible if it was instead written as:

public Event<MyEventHandler> SomethingHappened = default;

Then you could also substitute in custom events. I also see no reason why the += operation couldn't still be used for adding new handlers. So does this simply decrease flexibility or does the keyword offer anything else?

4
  • 1
  • The point of multiple subscriptions to a single event is that the subscriptions are individual and independent of each other. If you need 3 things to happen in a particular order, then you should have one subscription to the event, and do those three things inside the eventhandler.
    – Flater
    Mar 5, 2018 at 13:12
  • 1
    By your same logic, are virtual methods a mistake in C#? You don't need virtual or override or abstract. Just make a public field of delegate type, and when you want to change the method implementation, you assign a different delegate to the field. For that matter, why are there properties? Just have public fields of delegate type for the getters and setters. Are properties also a mistake? Mar 5, 2018 at 18:43
  • Everything in programming is a mistake. But, we are stuck with what we have.
    – user251748
    Mar 5, 2018 at 19:13

2 Answers 2

8

The point of an event is that it doesn't allow outsiders to get or overwrite all event handlers. An event essentially a specialized kind of property, which has add/remove instead of get/set.

The version you show is the analog of an auto-property. If you're not content with the default implementation of add/remove, write your own.

public event EventHandler MyEvent {
   add { ... } 
   remove { ... }
}

Events have nothing to do with allowing to add you multiple handlers, you can already do that using multicast delegates.

4

The main value of an event being a first class citizen in the .NET world (that I can think of) is that it is recognizable through reflection across language implementations. This allows one developer to create a full featured (possibly closed source) component and have another developer use it.

Events are mainly a component feature that allow interaction between pieces of software with minimum dependencies. This is also the reason for the standard handler prototype: (object sender, EventArgs e).

If this feels too restrictive and you do not care about unknown client or this scenario just does not apply to your situation, you can still create your own mechanism. But any developer that has to maintain your stuff after you will hate you for it.

2
  • Step 1: Look at the 1990 book "Programming Windows" by Thomas Petzold (available online) for an original C example of the Hello World Windows program. Step 2: Be glad for how things are done now!
    – user251748
    Mar 5, 2018 at 19:16
  • @no comprende It was Charles, not Thomas. Mar 6, 2018 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.