Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8250753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:00:18+00:00 2026-06-08T00:00:18+00:00

Basically this has been in my head for a while… and I’d like to

  • 0

Basically this has been in my head for a while… and I’d like to read your opinion

I read the great book from Jon Skeet called: C# in Depth, Second Edition and there’s a recommendation to use something like this when declaring custom events:

public event Action<string> MyEvent = delegate { };

This declaration will release us from the nullity check statement before firing the event, so instead of this:

    if (this.MyEvent != null)
    {
        this.MyEvent("OMG osh");
    }

We can simply call:

this.MyEvent("OMG osh");

And our code simply will work.

When you declare events like this, the event will be initialized with an empty delegate so we do not have the need to check for null.

This is another way to declare events, they are equivalent

private Action<string> myDelegate;
public event Action<string> MyEvent
{
    add
    {
        this.myDelegate += value;
    }
    remove
    {
        this.myDelegate -= value;
    }
}

For more info: http://csharpindepth.com/Articles/Chapter2/Events.aspx

I just had a discussion with some fellows, at work and we were discussing this topic, they were arguing that sometimes we would have the need to clear all the subscriptions for the event at once, we can simply do that assigning null to the delegate behind and if we want to keep using the same pattern we would have to re-initialize the event with an empty delegate. They were also asking what would happen in a multi-threading scenario, that’s the main reason I placed this question

Question

  1. I would like to know if there are some hidden implications (perhaps when using multi-threading) when declaring events following this pattern vs checking for null

  2. If I use this patter I’m effectively removing several if conditions, which result removing jump statements. Wouldn’t this be better in overall performance terms?

Cheers

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:00:21+00:00Added an answer on June 8, 2026 at 12:00 am

    First off: Are you sure that you are not prematurely optimising? Are you actually facing a performance issue because instead of a check for null, you are calling an additional empty method when the event is triggered? If not, then I’d suggest you drop this issue.

    Now, to your question: I think it’s fairly safe to say that a check for null is cheaper than a superfluous method invocation, so if you want the best-possible performance, then perhaps Jon Skeet’s convenient delegate { } pattern is not for you.

    When it comes to multi-threading, a much more important issue would be to define on which thread / in which context (e.g. SynchronizationContext) each event handler will be invoked. You should be able to leave this decision to each consumer of your event, as some event handlers won’t care, other will want to forward to the right context (e.g. by means of SynchronizationContext.Post).

    If you do decide on checks for null, then note that instead of this:

    if (this.MyEvent != null)
    {
        this.MyEvent("OMG osh");
    }
    

    it is recommended that in multi-threading scenarios, you do this instead:

    Action<string> handlers = this.MyEvent;
    if (handlers != null)
    {
        handlers("OMG osh");
    }
    

    That is, first copy the delegate into a local variable. This is because the delegate variable could be manipulated by another thread during the check for null. (I’m not so sure this is really necessary, but this is the suggested pattern nevertheless.)


    Off-topic: I think you got a minor point slightly wrong:

    [W]hen you declare an event, what [is] actually happening is that you are declaring a delegate and an event.

    That is correct, but it’s worth noting that to the CLI, events are simply a grouping of several methods (e.g. the add, remove, raise and possibly other accessors). Pretty much everything else is actually language-specific. For example, an event in C# is nothing other than a delegate, paired with some accessor methods, and some additional constraints on what can be done with the delegate (i.e. += and -= being the only valid operations on the delegate from outside the type where the event is declared).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just want to ask if anybody has done something like this. Basically, it's
I'm sure this has been covered but I can't find it anywhere, I basically
Im sure this has been asked 1000 times before, basically all I want to
I've been trying to wrap my head around this the whole day... Basically, I
Been scratching my head on this for a while.... I have a PDO object
So I've been basically beating my head against the wall on this for a
I have this page which has three peson searchs on them. Basically it's a
I hope this question is not 'controversial' - I'm just basically asking - has
I recently found this post . It basically says that Eclipse has a modified
This isn't a code question for once, but it definitely has me confused. Basically,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.