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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:25:43+00:00 2026-06-04T06:25:43+00:00

Is there a difference between these two implementations? 1 : public class SMSManager :

  • 0

Is there a difference between these two implementations?

1 :

public class SMSManager : ManagerBase
{
    private EventHandler<SheetButtonClickEventArgs> _buttonClickevent; 

    public SMSManager(DataBlock smsDataBlock, DataBlock telephonesDataBlock) :
        base(smsDataBlock)
    {
        _buttonClickevent = new EventHandler<SheetButtonClickEventArgs>(OnButtonClick);
        SheetEvents.ButtonClick += _buttonClickevent;

    }

    public override void Dispose()
    {
        base.Dispose();
        if (_buttonClickevent != null)
        SheetEvents.ButtonClick -= _buttonClickevent;
    }
}

2 :

public class SMSManager : ManagerBase
{
    public SMSManager(DataBlock smsDataBlock, DataBlock telephonesDataBlock) :
        base(smsDataBlock)
    {
        SheetEvents.ButtonClick += new EventHandler<SheetButtonClickEventArgs>(OnButtonClick);   
    }

    public override void Dispose()
    {
        base.Dispose();
        SheetEvents.ButtonClick -= new EventHandler<SheetButtonClickEventArgs>(OnButtonClick);
    }
}

The first one seems to be more correct than the second in regards to memory leaks. But is it really correct?

  • 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-04T06:25:44+00:00Added an answer on June 4, 2026 at 6:25 am

    The long and short of it is the second piece of code is correct and safe (even if there are no handlers registered).

    Consider this sample app:

    namespace ConsoleApplication61
    {
        class Program
        {
            static void Main(string[] args)
            {
                var f = new Foo();
                f.MyEvent += new EventHandler(Handler);
                f.Trigger();
                f.MyEvent -= new EventHandler(Handler);
                f.Trigger();
                Console.Read();
            }
    
            static void Handler(object sender, EventArgs e)
            {
                Console.WriteLine("handled");
            }
        }
    
        class Foo
        {
            public event EventHandler MyEvent;
            public void Trigger()
            {
                if (MyEvent != null)
                    MyEvent(null, null);
            }
        }
    }
    

    This sample prints “handled” once.

    So in your example, they are functionally the same and both will work as needed. Removing a handler that hasn’t been added is also a safe action, it simply finds nothing to remove and does nothing.

    As provided in the comments, Marc’s answer goes into more detail:

    Unregister events with new instance of the delegate


    Event Handlers with Anonymous Methods

    It is worth noting that event handlers in the form of lambda expressions are not guaranteed to enforce uniqueness based on instance and method signature. If you need to unsubscribe an anonymous method, you either need to promote it to a method or keep a reference to the anonymous method for later use:

    Func<object, EventArgs> meth = (s, e) => DoSomething();
    
    myEvent += meth;
    myEvent -= meth;
    

    Jon Skeet goes into detail answering this and likely does a better job of it than me 🙂

    How to remove a lambda event handler


    A Slight Refactoring

    I would refactor to the following:

    public class SMSManager : ManagerBase
    {
        public SMSManager(DataBlock smsDataBlock, DataBlock telephonesDataBlock) 
            : base(smsDataBlock)
        {
            SheetEvents.ButtonClick += OnButtonClick;   
        }
    
        public override void Dispose()
        {
            SheetEvents.ButtonClick -= OnButtonClick;
            base.Dispose();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any difference between these two LINQ statements: var query = from a
1)Is there any difference between these two keywords for the elements of collections??( Copy
What is the difference between these two class declarations? I don't understand why @class
I am not clear on the actual difference between these two styles of class
What are the main diffrences between these two implementations of LDAP protocol? Which is
Can someone explain the difference between these two, the first one is taken from
What is the difference between these two: font-style:italic font-style:oblique I tried using the W3Schools
What is the difference between these two functions? 1: $(document).ready(function myfunc() { function dosomething()
Does anybody know the difference between these two commands to switch and track a
What is the difference between these two RTC source control icons ? Is it

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.