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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:35:02+00:00 2026-05-14T18:35:02+00:00

Possible Duplicate: C#: Difference between ‘ += anEvent’ and ‘ += new EventHandler(anEvent)’ There

  • 0

Possible Duplicate:
C#: Difference between ‘ += anEvent’ and ‘ += new EventHandler(anEvent)’

There are two basic ways to subscribe to an event:

SomeEvent += new EventHandler<ArgType> (MyHandlerMethod);
SomeEvent += MyHandlerMethod;

What is the difference, and when should I chose one over the other?

Edit: If it is the same, then why does VS default to the long version, cluttering the code? That makes no sense at all to me.

  • 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-14T18:35:02+00:00Added an answer on May 14, 2026 at 6:35 pm

    Since there seemed to be some dispute over my original answer, I decided to do a few tests, including looking at the generated code and monitoring the performance.

    First of all, here’s our test bed, a class with a delegate and another class to consume it:

    class EventProducer
    {
        public void Raise()
        {
            var handler = EventRaised;
            if (handler != null)
                handler(this, EventArgs.Empty);
        }
    
        public event EventHandler EventRaised;
    }
    
    class Counter
    {
        long count = 0;
        EventProducer producer = new EventProducer();
    
        public void Count()
        {
            producer.EventRaised += CountEvent;
            producer.Raise();
            producer.EventRaised -= CountEvent;
        }
    
        public void CountWithNew()
        {
            producer.EventRaised += new EventHandler(CountEvent);
            producer.Raise();
            producer.EventRaised -= new EventHandler(CountEvent);
        }
    
        private void CountEvent(object sender, EventArgs e)
        {
            count++;
        }
    }
    

    First thing to do is look at the generated IL:

    .method public hidebysig instance void Count() cil managed
    {
        .maxstack 8
        L_0000: ldarg.0 
        L_0001: ldfld class DelegateTest.Program/EventProducer DelegateTest.Program/Counter::producer
        L_0006: ldarg.0 
        L_0007: ldftn instance void DelegateTest.Program/Counter::CountEvent(object, class [mscorlib]System.EventArgs)
        L_000d: newobj instance void [mscorlib]System.EventHandler::.ctor(object, native int)
        L_0012: callvirt instance void DelegateTest.Program/EventProducer::add_EventRaised(class [mscorlib]System.EventHandler)
        L_0017: ldarg.0 
        L_0018: ldfld class DelegateTest.Program/EventProducer DelegateTest.Program/Counter::producer
        L_001d: callvirt instance void DelegateTest.Program/EventProducer::Raise()
        L_0022: ldarg.0 
        L_0023: ldfld class DelegateTest.Program/EventProducer DelegateTest.Program/Counter::producer
        L_0028: ldarg.0 
        L_0029: ldftn instance void DelegateTest.Program/Counter::CountEvent(object, class [mscorlib]System.EventArgs)
        L_002f: newobj instance void [mscorlib]System.EventHandler::.ctor(object, native int)
        L_0034: callvirt instance void DelegateTest.Program/EventProducer::remove_EventRaised(class [mscorlib]System.EventHandler)
        L_0039: ret 
    }
    
    .method public hidebysig instance void CountWithNew() cil managed
    {
        .maxstack 8
        L_0000: ldarg.0 
        L_0001: ldfld class DelegateTest.Program/EventProducer DelegateTest.Program/Counter::producer
        L_0006: ldarg.0 
        L_0007: ldftn instance void DelegateTest.Program/Counter::CountEvent(object, class [mscorlib]System.EventArgs)
        L_000d: newobj instance void [mscorlib]System.EventHandler::.ctor(object, native int)
        L_0012: callvirt instance void DelegateTest.Program/EventProducer::add_EventRaised(class [mscorlib]System.EventHandler)
        L_0017: ldarg.0 
        L_0018: ldfld class DelegateTest.Program/EventProducer DelegateTest.Program/Counter::producer
        L_001d: callvirt instance void DelegateTest.Program/EventProducer::Raise()
        L_0022: ldarg.0 
        L_0023: ldfld class DelegateTest.Program/EventProducer DelegateTest.Program/Counter::producer
        L_0028: ldarg.0 
        L_0029: ldftn instance void DelegateTest.Program/Counter::CountEvent(object, class [mscorlib]System.EventArgs)
        L_002f: newobj instance void [mscorlib]System.EventHandler::.ctor(object, native int)
        L_0034: callvirt instance void DelegateTest.Program/EventProducer::remove_EventRaised(class [mscorlib]System.EventHandler)
        L_0039: ret 
    }
    

    So it turns out that, yes, these do generate identical IL. I was wrong originally. But that’s not the whole story. It may be that I’m going off-topic here but I think that it’s important to include this when talking about events and delegates:

    Creating and comparing different delegates is not cheap.

    When I wrote this, I was thinking that the first syntax was able to cast the method group as a delegate, but it turns out that it’s just a conversion. But it’s completely different when you actually save the delegate. If we add this to the consumer:

    class Counter
    {
        EventHandler savedEvent;
    
        public Counter()
        {
            savedEvent = CountEvent;
        }
    
        public void CountSaved()
        {
            producer.EventRaised += savedEvent;
            producer.Raise();
            producer.EventRaised -= savedEvent;
        }
    }
    

    You can see that this has very different characteristics, performance-wise, from the other two:

    static void Main(string[] args)
    {
        const int TestIterations = 10000000;
    
        TimeSpan countTime = TestCounter(c => c.Count());
        Console.WriteLine("Count: {0}", countTime);
    
        TimeSpan countWithNewTime = TestCounter(c => c.CountWithNew());
        Console.WriteLine("CountWithNew: {0}", countWithNewTime);
    
        TimeSpan countSavedTime = TestCounter(c => c.CountSaved());
        Console.WriteLine("CountSaved: {0}", countSavedTime);
    
        Console.ReadLine();
    }
    
    static TimeSpan TestCounter(Action<Counter> action, int iterations)
    {
        var counter = new Counter();
        Stopwatch sw = new Stopwatch();
        sw.Start();
        for (int i = 0; i < TestIterations; i++)
            action(counter);
        sw.Stop();
        return sw.Elapsed;
    }
    

    The results consistently come back as something similar to:

    Count: 00:00:02.4742007
    CountWithNew: 00:00:02.4272702
    CountSaved: 00:00:01.9810367
    

    That’s nearly a 20% difference when using a saved delegate vs. creating a new one.

    Now obviously not every program is going to be adding and removing this many delegates in such a small amount of time, but if you’re writing library classes – classes that might be used in ways you cannot predict – then you really want to keep this difference in mind if you ever need to add and remove events (and I’ve written a lot of code that does this, personally).

    So the conclusion of this is, writing SomeEvent += new EventHandler(NamedMethod) compiles to the same thing as just SomeEvent += NamedMethod. But if you plan to remove that event handler later, you really should save the delegate. Even though the Delegate class has some special-case code that allows you to remove a referentially-different delegate from the one you added, it has to do a non-trivial amount of work to pull this off.

    If you’re not going to save the delegate, then it makes no difference – the compiler ends up creating a new delegate anyway.

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

Sidebar

Ask A Question

Stats

  • Questions 435k
  • Answers 435k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Removing the invalid HTML, I can duplicate the issue, so… May 15, 2026 at 3:27 pm
  • Editorial Team
    Editorial Team added an answer You shouldn't release returned object unless they come from an… May 15, 2026 at 3:27 pm
  • Editorial Team
    Editorial Team added an answer Yes, the DOM is reliably ready at that point. This… May 15, 2026 at 3:27 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.