-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Description
The Events section describes how to implement the event ThresholdReached.
The method OnThresholdReached creates a variable and assigns it the event value.
The code makes you think that you could simplify the call from
EventHandler handler = ThresholdReached;
handler?.Invoke(this, e);
to
ThresholdReached?.Invoke(this, e);
However, the docs page How to: Raise Base Class Events in Derived Classes (C# Programming Guide) adds a comment to this statement (albeit it is hidden within quite a lot of code in the example code):
// Make a temporary copy of the event to avoid possibility of
// a race condition if the last subscriber unsubscribes
// immediately after the null check and before the event is raised.
I think it would be very helpful to educate users here why the copy is necessary/recommended. I think it should also link to the other page so users can learn why the protected virtual method is used to raise events.
The fact that the assignment operator = makes event copies rather than references was unexpected to me, which would also be a helpful clarification here.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: caf84f7a-743b-867a-2ac5-2ab815f10c45
- Version Independent ID: 9b1d1a8a-5151-8e4d-2365-7c859e1bacd8
- Content: Handling and Raising Events
- Content Source: docs/standard/events/index.md
- Product: dotnet
- Technology: dotnet-standard
- GitHub Login: @rpetrusha
- Microsoft Alias: ronpet