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 8236479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:08:51+00:00 2026-06-07T19:08:51+00:00

Considering the following basic event-related code inside a class: public event EventHandler Updated; public

  • 0

Considering the following basic event-related code inside a class:

public event EventHandler Updated;

public void OnUpdated() {
    if (Updated != null) Updated(sender: this, e: null)
}

If no one has subscribed to the Updated event, I would not like the fourth line to cause any significant performance drag (the idea is to let subscribers choose the most fine-grained events to subscribe to have only the minimal number of events fired and prevent a message queue overload).

Should I care and track the existence of subscribers (e.g. with if (Updated != null && OnUpdateSubscribed) Updated(sender: this, e: null) or trust the compiler/runtime?

  • 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-07T19:08:53+00:00Added an answer on June 7, 2026 at 7:08 pm

    The check OnUpdate != null defines an event being subscribed / unsubscribed. In fact, there is no real difference between a boolean check and a null check, because ultimately both of them are just a “load field”, “branch if false” – because a null reference counts as “false” as far as logic checks are concerned.

    My only suggestion would be: store it in a local variable, to prevent a (pretty unlikely, but possible) race condition:

    var snapshot = Updated;
    if(snapshot != null) snapshot(this, EventArgs.Empty);
    

    So: no, basically: there is no overhead of this.

    Example:

    public event EventHandler SomeEvent;
    protected virtual void OnSomeEvent()
    {
        var snapshot = SomeEvent;
        if (snapshot != null) snapshot(this, EventArgs.Empty);
    }
    

    Compiles to (comments are mine):

    .method family hidebysig newslot virtual instance void OnSomeEvent() cil managed
    {
        .maxstack 3
        .locals init (
            [0] class [mscorlib]System.EventHandler snapshot)
    
        // var snapshot = SomeEvent
        L_0000: ldarg.0 
        L_0001: ldfld class [mscorlib]System.EventHandler Foo::SomeEvent
        L_0006: stloc.0
    
        // if(snapshot == null) goto L_0016;
        L_0007: ldloc.0 
        L_0008: brfalse.s L_0016
    
        // snapshot(this, EventArgs.Empty);
        L_000a: ldloc.0 
        L_000b: ldarg.0 
        L_000c: ldsfld class [mscorlib]System.EventArgs [mscorlib]System.EventArgs::Empty
        L_0011: callvirt instance void [mscorlib]System.EventHandler::Invoke(object, class [mscorlib]System.EventArgs)
    
        // L_0016: return;
        L_0016: ret 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Considering following code public class A { public static void main(String[] args) { new
Considering following code: class Results { public int playerId; public int score; public int
Considering that simple java code which would not work: public class Bar extends AbstractBar{
Considering the following PHP class: class someObject { public function broken(){ return isset($this->something()) ?
Considering the following code public bool GetFalse() { return false; } public bool GetTrue()
Considering the following code public interface IEntity { int Id { get; set; }
Considering the following (pointless, but it's for illustration purpose) test class : public class
Considering the following case in C#: class Foo { public Foo() { } }
considering the following enum: public enum LeadStatus { Cold = 1, Warm = 2,
quick question on GWT from a newbie. Considering the following code: FlowPanel block =

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.