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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:33:49+00:00 2026-06-15T04:33:49+00:00

We can raise event in two ways: public event EventHandler MyEvent; private void DoSomething()

  • 0

We can raise event in two ways:

public event EventHandler MyEvent;

private void DoSomething()
{
    ...
    var handler = MyEvent;
    if (handler != null)
        handler(this,EventArgs.Empty);
}

and

public event EventHandler MyEvent = (o,e) => {} ;

private void DoSomething()
{
    ...
    MyEvent(this, EventArgs.Empty);
}

I prefer the last one. It is shorter.
My colleagues insist on the first variant.

Is there any superiority of the first over the second one?

  • 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-15T04:33:50+00:00Added an answer on June 15, 2026 at 4:33 am

    Update for C# 6

    In C# 6 you simply use the null-conditional operator like so:

    PropertyChanged?.Invoke(this, args);
    

    This is recommended by the Roslyn wiki

    Original Answer

    Eric Lippert has a great blog post on Events and Races, which you should read if you haven’t.

    The first option could be considered safer than the second because the event could get set to null. Someone could carelessly modify the class. Also, if you deserialize instances the 2nd method won’t work (depending on the serialization mechanism you use).

    I sometimes use a helper method to raise events

    static class Raiser
    {
        public static void Raise<T>(this EventHandler<T> evnt, object sender, T args)
            where T : EventArgs
        {
            if (evnt != null)
            {
                evnt(sender, args);
            }
        }
    }
    
    class SomeClass
    {
        public event EventHandler<EventArgs> MyEvent;
    
        private void DoSomething()
        {
            MyEvent.Raise(this, EventArgs.Empty);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I raise an event while clicking a textbox? I'm having trouble finding
I know that it's a subject that can raise a lot of debate, but
Can I change the field public virtual ClassOne ClassOne { get; set; } to
I'm trying to create a custom control and need to raise an event from
CancelEventArgs provides a Property called Cancel that different event handlers can set to tell
I have this little test-case which is supposed to show two widgets, with one
Icommand contains two methods and one event. What the two methods do is clear,
Problem : in my domain layer assembly I store two interfaces: public interface IDomainEvent
Two different slider controls fires this function below, their names are seektomediaposition and seektomediaposition2.
Python 2.7's argparse gives you two extension points where you can control how your

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.