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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:49:31+00:00 2026-06-01T06:49:31+00:00

Just finished reading Jon Skeet’s article about events and delegates and got a question.

  • 0

Just finished reading Jon Skeet’s article about events and delegates and got a question.

Lets say first in the code I declare an event

public event EventHandler MyEvent

Then I want to raise it in the code in a manner

if (MyEvent != null)
    Myevent(this,EvtArgs.Empty);

Jon says that in fact MyEvent looks somehow like this:

private EventHandler _myEvent;

public event EventHandler MyEvent
{
    add
    {
        lock (this)
        {
            _myEvent += value;
        }
    }
    remove
    {
        lock (this)
        {
            _myEvent -= value;
        }
    }        
} 

The question is what really happens when I compare MyEvent != null ?
As I understand in fact it compares _myEvent to null, but I am not sure.

  • 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-01T06:49:32+00:00Added an answer on June 1, 2026 at 6:49 am

    If you implement custom add/remove accessors, you won’t be able to compare MyEvent to null in the first place, because it will only be an event – it doesn’t have a value as such, only add/remove accessors. You would have to use your declared field (_myEvent in the sample above) instead.

    You can only do the comparison using the event name when you’re using field-like events, where you end up with (effectively) a field and an event with the same name. (The compiled code doesn’t have to actually reuse the event name for the field name, but it has to look like it does when you’re compiling.)

    Note that using this:

    if (MyEvent != null)
        MyEvent(this,EvtArgs.Empty);
    

    isn’t thread-safe, as MyEvent could become null between the check and the invocation. You should use:

    EventHandler handler = MyEvent;
    if (handler != null)
    {
        handler(this, EventArgs.Empty);
    }
    

    Also note that the part about field-like events locking on this is now slightly outdated; in C# 4 thread-safety is achieved using a lock-free compare-exchange mechanism.

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

Sidebar

Related Questions

Just finished reading this blog post: http://www.skorks.com/2010/03/an-interview-question-that-prints-out-its-own-source-code-in-ruby/ In it, the author argues the case
I just finished reading about scoping in the R intro , and am very
I've just finished reading up on the CustomValidator control and I have a question.
After reading this SO question and noting the consensus about just how evil Thread.Sleep()
Just finished reading Crockford's JavaScript: The Good Parts and I have a question concerning
Just finished reading the latest stackoverflow blog post about moving static content to a
I just finished reading this article on the advantages and disadvantages of exceptions and
I've just finished reading about SOAP via Spring-WS in Spring in Action, 2nd edition,
I am just about finished reading K&R, and that is all the C that
I have just finished reading an article on MSDN by John Evdemon . He

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.