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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:43:05+00:00 2026-05-23T13:43:05+00:00

I set an lambda expression as a event handler to a event proxy.SomeEvent +=

  • 0

I set an lambda expression as a event handler to a event

proxy.SomeEvent += (sender, e) => { doSomething(); };

But when debugging I found doSomething() was executed twice because somehow the above event assignment statement was executed twice.

So I want check whether the proxy.SomeEvent is null before calling the event assignment statement,like:

if(proxy.SomeEvent == null)
{
    proxy.SomeEvent += (sender, e)=> { doSomething(); };
}

But I get a compiler error The event ‘Proxy.SomeEvent’ can only appear on the left hand side of += or -=

Since it is not possible to remove a lambda expression event handler by the -= operator, is there other way which enables me to check whether an event has already been assigned?

  • 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-23T13:43:06+00:00Added an answer on May 23, 2026 at 1:43 pm

    Your specific question was how to check to see if your lambda was already registered in order to avoid registering it twice. In the past I where I did not what to declare a separate method (which would support “-=”), I just assigned the lambda to a local variable before subscribing to the event.

    public class SomeOtherClass
    {
        public void ResponseToSomeEvent()
        {
            var proxy = new Proxy();
    
            // Assign the lambda to a local variable
            EventHandler doSomething = (sender, e) => Console.WriteLine("Just Once");
    
            // Subscribe to event
            proxy.SomeEvent += doSomething;
    
            proxy.Raise();
    
            // Unsubscribe and resubscribe to event
            proxy.SomeEvent -= doSomething;
            proxy.SomeEvent += doSomething;
    
            proxy.Raise();
        }
    }
    
    public class Proxy
    {
        public event EventHandler SomeEvent;
    
        public void Raise()
        {
            Console.WriteLine("Raise");
            SomeEvent(this, EventArgs.Empty);
        }
    }
    

    The result is as expected, the lambda is only invoked once each time the event is raised, because the -= is effectively able to remove the subscription since you were able to provide the event handler to remove.

    However, in more complex scenarios you may be using a lambda to perform a closure, and due to code paths you cannot easily maintain a reference to the original lambda that you used to subscribe to the event. If your case is this complex, I recommend creating a concrete closure class (similar to what the C# compiler does) and subscribe to the event using a method on an instance of your closure class. You will then need to override equals on your closure class to determine that the event subscription is the same based on the closure input values. This allows you to safely subscribe with one instance of your closure class and unsubscribe/resubscribe with a different instance at some later point.

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

Sidebar

Related Questions

I set up 404 handler page in web.config, but it works ONLY when extension
For example: _ctx.DataContext.Set<ParentClass>().Include(ChildCollection).OrderBy(...) Everytime I put a Lambda expression in the OrderBy clause I
I set a passphrase when creating a new SSH key on my laptop. But,
I was wiring up an event to use a lambda which needed to remove
When creating a lambda expression by hand I get a 'Parameter not in scope'
Is it possible to assign an Lambda Expression (action?) to a property of my
How to use lambda expression as a template parameter? E.g. as a comparison class
#include <iostream> #include <set> #include <algorithm> #include <boost/lambda/lambda.hpp> #include <boost/bind.hpp> using namespace std; using
I'm trying to use a method group in a lambda expression, like this: public
Hello question on like condition using lambda expression. I hope you can reply to

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.