PropertyChangedEventManager will call a function but NOT a lambda. Why?

101 Views Asked by At

In my C# app (.NET 6) One of my class' constructors hooks up to a PropertyChanged notification of one of the arguments passed in. I did it like this:

public ProfileSource(ISystemService sys, Routine routine, Aspect aspect)
    : base(sys)
{
    Routine = routine;
    Aspect = aspect;

    PropertyChangedEventManager.AddHandler(
        Routine,
        (_, _) =>
        {
            RaisePropertyChanged(nameof(Profiles));
        },
        nameof(IRoutine.AreResultsValid));
}

In Debug mode I was was surprised to find my lambda was not being called when the Routine object raises that property changed for its AreResultsValid property.

But when I changed that lambda to instead be a member function of the class, suddenly it started working.

public ProfileSource(ISystemService sys, Routine routine, Aspect aspect)
    : base(sys)
{
    Routine = routine;
    Aspect = aspect;

    PropertyChangedEventManager.AddHandler(
        Routine,
        OnResultsValidChanged,
        nameof(IRoutine.AreResultsValid));
}

private void OnResultsValidChanged(object? sender, PropertyChangedEventArgs args)
{
    RaisePropertyChanged(nameof(Profiles));
}

With this change (and no other), my handler is called every time.

I can switch back and forth and observe the effect. Lambda version: handler not called. Function version: handler called

Can someone tell me why this would be? What am I missing here?

(Edited to add: I use the lambda approach in several other places and it's always worked. Now I'm starting to worry that i need to go and change those all to be functions, just in case)

0

There are 0 best solutions below