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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:01:49+00:00 2026-05-25T22:01:49+00:00

Often I see code like this scattered and duplicated around source code: var handler

  • 0

Often I see code like this scattered and duplicated around source code:

var handler = MyEvent;

if (handler != null)
{
    handler.Invoke(null, e);
}

Is there any reason not to just encapsulate it within an extension method like this?

public static void SafeInvoke<T>(this EventHandler<T> theEvent, object sender, T e) where T : EventArgs
{
    var handler = theEvent;

    if (handler != null)
    {
        handler.Invoke(sender, e);
    }
}

So that calls can just be made like so:

MyEvent.SafeInvoke(this, new MyEventArgs(myData));

  • 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-05-25T22:01:49+00:00Added an answer on May 25, 2026 at 10:01 pm

    This is an interesting one. Jeffrey Richter talsk a lot about this in CLR via C#.

    With a null check like the following:-

    var handler = MyEvent;
    
    if (handler != null)
    {
        handler.Invoke(null, e);
    }
    

    The JIT compiler could potentially optimise away the handler variable entirely. However, the CLR team are aware that many developers use this pattern for raising events and have built this awareness into the JIT compiler. It is likely that this will remain in place in all future versions of the CLR because changing the behaviour might break too many existing applications.

    There is no reason that you can’t write an extension method to do this work for you (this is exactly what I have done). Note that the method itself may be JIT inlined (but the temporary variable still won’t be optimised away).

    If you envisage your application being used against another runtime, e.g. Mono (which I suspect mirrors the CLR’s behaviour in this case anyway) or some other exotic runtime which may appear, then you can guard your code against the possibility of JIT inlining by adding [MethodImplAttribute(MethodImplOptions.NoInlining)] to your extension method.

    If you decide not to use an extension method, another way to protect against JIT optimisation (which Jeffrey Richter recommends) is to assign the value to the temporary variable in a way which cannot be optimised away by the JIT compiler, e.g.

    var handler = Interlocked.CompareExchange(ref MyEvent, null, null);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm still learning ASP.NET and I often see code like this throughout parts of
Often I find myself writing code like this: if (Session != null) { Session.KillAllProcesses();
In jquery scripts i often see functions called like this: var somefunction = function(){
In threads when dealing with cancelation, you often you see code like this while
I often see code like this: // Approach 1 if(data != nil){ // Do
I often see the code which uses mock in Rspec, like this: describe GET
I often see code like: Iterator i = list.iterator(); while(i.hasNext()) { ... } but
I often see code like int hashCode(){ return a^b; } Why XOR?
You often see, on sites like The Daily WTF , examples of overengineered code
I've see this sort of thing in Java code quite often... try { fileStream.close();

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.