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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:59:27+00:00 2026-06-15T02:59:27+00:00

Whenever you add a delegate to an event handler, you should remove it later,

  • 0

Whenever you add a delegate to an event handler, you should remove it later, right? So if you attach an anonymous method to an event, does this create an event handler leak since you can’t later remove it? This code sample from http://msdn.microsoft.com/en-us/library/0yw3tz5k%28VS.80%29.aspx seems to imply that this an okay practice though.

// Create a handler for a click event
button1.Click += delegate(System.Object o, System.EventArgs e)
                   { System.Windows.Forms.MessageBox.Show("Click!"); };

Is this really an okay practice?

  • 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-15T02:59:29+00:00Added an answer on June 15, 2026 at 2:59 am

    Whenever you add a delegate to an event handler, you should remove it later, right?

    Not necessarily, no. Often you want the event handler to stay valid for as long as the event itself can be raised – that’s certainly very common with UIs.

    Is this really an okay practice?

    Absolutely, so long as you don’t need to unhook the handler. Think about the point at which you’d unhook the event handler. If it’s “when the form (or button, or whatever) is elegible for garbage collection” then what benefit is there in removing the handler? Just let it be garbage collected with the form…

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

Sidebar

Related Questions

CKEditor does this whenever I add a heading tag: <h2> Mai 2010</h2> How can
I know that whenever you add/remove/modify any file in the App_Code, App_GlobalResources, and bin
Okay So I create a ListView and my own ListAdapter but whenever I add
Whenever We add id attribute to the <img> tag, the image does not show
Using IntelliJ , whenever I add implements Serializable it does not offer to add
I'm using the acts_as_list plugin with rails 3.1 and whenever I add or remove
Whenever I add an event listener window.onbeforeunload , can I check where the page
This is strange, whenever I add using namespace ... to a header file it
Whenever I add paddingRight to a column in the flex grid, it adds the
whenever I add a new domain into my new Plesk control panel on my

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.