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

  • Home
  • SEARCH
  • 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 5959435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:41:03+00:00 2026-05-22T18:41:03+00:00

I would like to hear opinions on below code snippet. Is there anything that

  • 0

I would like to hear opinions on below code snippet. Is there anything that can be improved? Is the event handler/raiser naming following best practices? I know it is not that useful to handle and raise events in the same class but this is just a snippet.

public class MyControl
{
    public MyControl()
    {
        this.LogWritten += this.HandleMyControlLogWritten;
    }

    // Event handler
    void HandleMyControlLogWritten(object sender, EventArgs e)
    {
    }

    // Event object
    public event Action<object, EventArgs> LogWritten;

    // Event raiser
    protected virtual void OnLogWritten(EventArgs e)
    {
        if (this.LogWritten != null)
        {
            this.LogWritten(this, e);
        }
    }
}
  • 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-22T18:41:04+00:00Added an answer on May 22, 2026 at 6:41 pm

    The main change I’d recommend would be to get a copy of the event handler:

    // Event raiser
    protected virtual void OnLogWritten(EventArgs e)
    {
        var handler = this.LogWritten;
        if (handler != null)
        {
            handler(this, e);
        }
    }
    

    This is important if you’re planning to (eventually) use this class in a multi-threaded scenario. As such, I find that it’s a good “best practice” to get into the habit of using. The issue is that, when using in multiple threads, without creating the copy, it’s possible that the only “handler” attached could unsubscribe between the null check and the invocation, which would cause a runtime error. By copying to a temporary variable (the var handler = this.LogWritten;) line, you’re effectively creating a “snapshot” of the subscriber list, and then checking it for null and invoking if required.

    The other change is in the event declaration itself. Instead of using Action<T1,T2>:

    // Event object
    public event Action<object, EventArgs> LogWritten;
    

    I would recommend using EventHandler<TEventArgs> (if you want to use a custom EventArgs subclass) or EventHandler (for standard EventArgs). These are more “standard practice”, and will be what other developers expect:

    // Event object
    public event EventHandler LogWritten;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to hear opinions on small amounts of code replication within methods
That is a common question, but I would like to hear some expert opinions.
I would like to hear opinions or peoples experiences regarding Rel . Is it
I would like to hear some opinions about using the isolated storage in Silverlight
I would like to hear some opinions on hand coding your GUIs as one
I would like to hear from you folks that have achieved a high-level of
I would like to hear some opinions on window strategy creation with MVC. I'm
I just would like to hear different opinions about ROWID type usage as input
I would like to hear of the experiences of classic vb developers who migrated
I would like to hear other people's advice on when one should build a

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.