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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:03:08+00:00 2026-05-12T11:03:08+00:00

I recently discovered that I can use lambdas to create simple event handlers. I

  • 0

I recently discovered that I can use lambdas to create simple event handlers. I could for example subscribe to a click event like this:

button.Click += (s, e) => MessageBox.Show("Woho");

But how would you unsubscribe it?

  • 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-12T11:03:08+00:00Added an answer on May 12, 2026 at 11:03 am

    The C# specification explicitly states (IIRC) that if you have two anonymous functions (anonymous methods or lambda expressions) it may or may not create equal delegates from that code. (Two delegates are equal if they have equal targets and refer to the same methods.)

    To be sure, you’d need to remember the delegate instance you used:

    EventHandler handler = (s, e) => MessageBox.Show("Woho");
    
    button.Click += handler;
    ...
    button.Click -= handler;
    

    (I can’t find the relevant bit of the spec, but I’d be quite surprised to see the C# compiler aggressively try to create equal delegates. It would certainly be unwise to rely on it.)

    If you don’t want to do that, you’ll need to extract a method:

    public void ShowWoho(object sender, EventArgs e)
    {
         MessageBox.Show("Woho");
    }
    
    ...
    
    button.Click += ShowWoho;
    ...
    button.Click -= ShowWoho;
    

    If you want to create an event handler which removes itself using a lambda expression, it’s slightly trickier – you need to refer to the delegate within the lambda expression itself, and you can’t do that with a simple “declare a local variable and assign to it using a lambda expression” because then the variable isn’t definitely assigned. You typically get around this by assigning a null value to the variable first:

    EventHandler handler = null;
    handler = (sender, args) =>
    {
        button.Click -= handler; // Unsubscribe
        // Add your one-time-only code here
    }
    button.Click += handler;
    

    Unfortunately it’s not even easy to encapsulate this into a method, because events aren’t cleanly represented. The closest you could come would be something like:

    button.Click += Delegates.AutoUnsubscribe<EventHandler>((sender, args) =>
    {
        // One-time code here
    }, handler => button.Click -= handler);
    

    Even that would be tricky to implement within Delegates.AutoUnsubscribe because you’d have to create a new EventHandler (which would be just a generic type argument). Doable, but messy.

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

Sidebar

Related Questions

So I recently discovered that I could use <>...</> tags in javascript in Firefox,
I've recently discovered that you can use SUBTOTAL for various functions which allow you
I recently discovered that I could use the sp_help to get a table definition
I recently discovered that one can copy over an assembly that is in use
I recently discovered that in C++ you can overload the function call operator, in
Recently I ran into trouble when I discovered that vista restricts what can be
I recently discovered at work that it is the policy not to use compiler
I recently discovered that you can conditionally assign a value with an if-else block.
I recently discovered that a method in a derived class can only access the
I have discovered recently that's possible to call anoymous blocks from jdbc like this:

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.