C # Standard: Justification for "Do Not Provide Variables for a Participant in a Public Event"?

The IDesign coding standard states: "Do not provide member variables for a public event. Use event assemblers instead."

I understand that there are situations where event accessors are useful (I think Control uses the dictionary only to store events that are assigned to save memory). But what's the point of having this boilerplate code always exist?

Edit: Explicitly

 public event EventHandler EventName; 

vs.

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

[cm. IDesign Coding Standard - Coding Standards - 42]

+4
source share
1 answer

Delegation fields are not events - they are just delegate fields. They can be (very incorrectly) checked, encoded and called by external parties, which contradicts the design of events (which can usually be processed by the declaring class, usually).

Encapsulation allows indirect, confirmation, etc .; everything is desirable. And the cost is minimal. For this, it would not be normal to affect the performance of your code.

It also allows you to use them in interfaces ; fields cannot be declared on interfaces.


Edit:

The question is a bit unclear, but if the question is just field events versus explicit add / remove with delegate support ... Then it's stupid. Do not add extra code. An event-like field would be preferable if there weren’t enough events to guarantee the implementation of EventHandlerList.

In particular, the 4.0 compiler works great with streaming security (this has not been historically).

+3
source

Source: https://habr.com/ru/post/1347119/


All Articles