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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:21:36+00:00 2026-06-06T07:21:36+00:00

I want to know how to unsubscribe anonymous methods from the events. I have

  • 0

I want to know how to unsubscribe anonymous methods from the events.

I have already checked Unsubscribe anonymous method in C# but my case is little different.

I am accessing local function variable in the anonymous method.

Code is as below

 private static void Test(Object dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        if (dependencyPropertyChangedEventArgs.OldValue is ObservableCollection<object>)
        {
            (dependencyPropertyChangedEventArgs.OldValue as ObservableCollection<object>).CollectionChanged -=
                (s, e) => SelectedItemsChanged(dependencyObject, e); // TO FIX event unbsubscription via anonymous delegate
        }

        if (dependencyPropertyChangedEventArgs.NewValue is ObservableCollection<object>)
        {
            (dependencyPropertyChangedEventArgs.NewValue as ObservableCollection<object>).CollectionChanged +=
                (s, e) => SelectedItemsChanged(dependencyObject, e);
        }
    }
  • 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-06T07:21:40+00:00Added an answer on June 6, 2026 at 7:21 am

    New answer, now the question has changed

    You can’t, basically. Your handler depends on dependencyObject, which will be captured in a new object on each call, so you’ll end up with unequal delegates.

    You could create a new class which holds the dependency object and overrides Equals to compare those objects instead of using anonymous functions, or you could just hold a reference to the previously-subscribed handler.


    Old answer, when the delegate didn’t depend on the parameters

    The bizarre thing is that in this particular case, it looks like you’re not capturing any local variables. So it’s possible that if the only place you’re subscribing to the event is from this method, you could get away with:

    private static void Test(Object a, DependencyPropertyChangedEventArgs args)
    {
        NotifyCollectionChangedEventHandler handler = 
            (s, e) => SelectedItemsChanged(dependencyObject, e);
    
        var oldObservable = args.OldValue as ObservableCollection<object>;
        if (oldObservable != null)
        {
            oldObservable.CollectionChanged -= handler;
        }
        var newObservable = args.NewValue as ObservableCollection<object>;
        if (newObservable != null)
        {
            newObservable.CollectionChanged += handler;
        }
    }
    

    As you’ve only got one lambda expression now, I’d expect that to be converted into a single static method, so the freshly-created delegate will be equal to the original one.

    However, I wouldn’t recommend that. I’d just create the method yourself, and then use a method group conversion.

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

Sidebar

Related Questions

I have video view it playing videos correctly but i want know video starting
as titled. I have use synchronized to guard multi-thread write. but I want know
I have an android app it displays web view.I want know the app idle
I know this is a duplicate question. but i want know it with an
I want know, what regular expression should I have for my string. My string
Hi friends i want know sectionIndexTitlesForTableView Action is works i have the following code...
I have a field with a datepicker. I want know if it is open.
i want know how i can manage multiple twitter account on iOS in my
i want know if is possible, to get a specific element value of a
please help me. I want know about what types of flags to run an

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.