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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:46:17+00:00 2026-05-11T03:46:17+00:00

I have a class that handles events from a WinForms control. Based on what

  • 0

I have a class that handles events from a WinForms control. Based on what the user is doing, I am deferencing one instance of the class and creating a new one to handle the same event. I need to unsubscribe the old instance from the event first – easy enough. I’d like to do this in a non-proprietary manner if possible, and it seems like this is a job for IDisposable. However, most documentation recommends IDisposable only when using unmanaged resources, which does not apply here.

If I implement IDisposable and unsubscribe from the event in Dispose(), am I perverting its intention? Should I instead provide an Unsubscribe() function and call that?


Edit: Here’s some dummy code that kind of shows what I’m doing (using IDisposable). My actual implementation is related to some proprietary data binding (long story).

class EventListener : IDisposable {     private TextBox m_textBox;      public EventListener(TextBox textBox)     {         m_textBox = textBox;         textBox.TextChanged += new EventHandler(textBox_TextChanged);     }      void textBox_TextChanged(object sender, EventArgs e)     {         // do something     }      public void Dispose()     {         m_textBox.TextChanged -= new EventHandler(textBox_TextChanged);     } }  class MyClass {     EventListener m_eventListener = null;     TextBox m_textBox = new TextBox();      void SetEventListener()     {         if (m_eventListener != null) m_eventListener.Dispose();         m_eventListener = new EventListener(m_textBox);     } } 

In the actual code, the ‘EventListener’ class is more involved, and each instance is uniquely significant. I use these in a collection, and create/destroy them as the user clicks around.


Conclusion

I’m accepting gbjbaanb’s answer, at least for now. I feel that the benefit of using a familiar interface outweighs any possible downside of using it where no unmanaged code is involved (how would a user of this object even know that?).

If anyone disagrees – please post/comment/edit. If a better argument can be made against IDisposable, then I’ll change the accepted answer.

  • 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. 2026-05-11T03:46:18+00:00Added an answer on May 11, 2026 at 3:46 am

    Yes, go for it. Although some people think IDisposable is implemented only for unmanaged resources, this is not the case – unmanaged resources just happens to be the biggest win, and most obvious reason to implement it. I think its acquired this idea because people couldn’t think of any other reason to use it. Its not like a finaliser which is a performance problem and not easy for the GC to handle well.

    Put any tidy-up code in your dispose method. It’ll be clearer, cleaner and significantly more likely to prevent memory leaks and a damn sight easier to use correctly than trying to remember to un-do your references.

    The intention of IDisposable is to make your code work better without you having to do lots of manual work. Use its power in your favour and get over some artificial ‘design intention’ nonsense.

    I remember it was difficult enough to persuade Microsoft of the usefulness of deterministic finalisation back when .NET first came out – we won the battle and persuaded them to add it (even if it was only a design pattern at the time), use it!

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

Sidebar

Ask A Question

Stats

  • Questions 356k
  • Answers 356k
  • 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 KeyValuePair is not serializable, because it has read-only properties. Here… May 14, 2026 at 8:56 am
  • Editorial Team
    Editorial Team added an answer I wonder if you would be better off creating a… May 14, 2026 at 8:56 am
  • Editorial Team
    Editorial Team added an answer Use WATIN. Very, very similar to the IE browser control,… May 14, 2026 at 8:56 am

Related Questions

I'm currently writing an ASP.Net app from the UI down. I'm implementing an MVP
I just wanted to see if I could have your thoughts on the design
This seems like a fundamentally simple question. I have a WinForms dialog box with
I am attempting to use MVP in WinForms and am confused on how to

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.