3

I am reading the .NET documentation on EventHandlers and do not understand this snippet of code from https://learn.microsoft.com/en-us/dotnet/standard/events/ :

class Counter
{
    public event EventHandler ThresholdReached;

    protected virtual void OnThresholdReached(EventArgs e)
    {
        EventHandler handler = ThresholdReached;
        handler?.Invoke(this, e);
    }

    // provide remaining implementation for the class
}

What is the purpose of local variable handler? Why not invoke TresholdReached directly TresholdReached?.Invoke(this, e); ? Similar examples are here: https://learn.microsoft.com/en-us/dotnet/api/system.eventhandler-1?view=net-5.0

EpsilonDelta
  • 51
  • 1
  • 5
  • 2
    _What is the purpose of the local variable `handler`_, **None** in this case, it is just a short example to show that `event` objects are strongly typed. I think the compiler will get rid of the local variable anyways. – Arthur Attout Jan 01 '21 at 17:13

2 Answers2

3

What is the purpose of local variable handler?

Probably none. There is certainly no reason in terms of code-correctness. It appears to be a holdover from the old, pre-null-conditional operator version of the documentation, in which copying the event field ThresholdReached into a local variable was required in order to safely resolve race conditions between raising the event and any other code that might be unsubscribing from it. As you point out, using the null-conditional operator, the code can simply be one line, invoking via the event field itself.

I only say "probably" because I guess there's a very small chance that the author of the documentation was hoping to achieve some other pedagogical goal with the more-explicit form of the code. But I can't imagine what that might be, and it's obviously interfering with general comprehension of the code (i.e. distracting from the important parts).

I submitted a comment on the page to let Microsoft know of the problem. I think there's also probably a place in the Microsoft Git repo where you can submit an issue for the documentation, if you like.

My experience has been that either way the problem is conveyed to Microsoft, they typically fix the problem in relatively short order. I would guess that in the next couple of weeks, that passage will be fixed, and this question will no longer be relevant. :)

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • So, when using null-conditional operator instead of != null there are no race hazards? (How so? Is there some article about it?) – EpsilonDelta Jan 01 '21 at 17:40
  • Correct. See e.g. https://stackoverflow.com/a/32421459, https://stackoverflow.com/a/32421409, https://stackoverflow.com/a/32734529, https://stackoverflow.com/a/36425978, and https://stackoverflow.com/a/37338166, for example. – Peter Duniho Jan 01 '21 at 17:43
  • @CamiloTerevinto: no, it couldn't, because comments are not to be used for answering the question. It even says right in the prompt: _"Avoid answering questions in comments"_. It's certainly not an opinion to state whether superfluous code is in fact superfluous, from a technical point of view (note that I made no attempt to address any _non-technical_ aspect, as is right). – Peter Duniho Jan 01 '21 at 17:51
1

Finally, I found the answer almost immediately after posting this question (that bugged me for days). Reading @lc.'s answer with comments to this questions: Events - naming convention and style led me to this page: https://learn.microsoft.com/en-us/archive/blogs/ericlippert/events-and-races

So the answer is: local handler variable effectively copies the immutable delegate and renders it thread safe. They could have mention it on that doc page.

EDIT: See @Peter Duniho answer and comments.

EpsilonDelta
  • 51
  • 1
  • 5
  • _"So the answer is: local handler variable effectively copies the immutable delegate and renders it thread safe"_ -- that's not really an answer, since the code would already be thread safe _without_ the local variable copy. Indeed, that seemed to be the crux of your question in the first place. I don't see how out-of-date documentation or blog articles would address the question that specifically pertains to whether the out-of-date technique is still relevant or not. – Peter Duniho Jan 01 '21 at 17:40
  • Copying an event handler instance is not thread safe to begin with. – Blindy Jan 01 '21 at 18:04