Loading user information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading user information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements


andreas_f andreas_f

Niner since 2011

  • Rx Workshop: Observables versus Events

    Here is my preferred solution for Events based on Valentinkuzub's implementation:

    class Events
            private int m_LastLength;
            public event Action<string> TextChanged;
            private Action<int> m_LengthChanged;
            public Events()
                TextChanged += s =>
                                       var length = (s ?? string.Empty).Length;
                                       if (length != m_LastLength)
                                           m_LastLength = length;
            public virtual void OnTextChanged(string text)
                var t = TextChanged;
                if (t != null)
            public event Action<int> LengthChanged
                add { m_LengthChanged += value; }
                remove { m_LengthChanged -= value; }



  • Rx Workshop: Observables versus Events

    Events code is implemented correctly by Kurator and Valentinkuzub.

    Kurator's implementation leaks memory as it adds a new delegate (lambda) to TextChanged on each call to add_LengthChanged without removing it in remove_LengthChanged. Valentinkuzub's implementation is nice even though it does some unnecessary work by updating the m_LastString variable multiple times if more than one subscriber registers with LengthChanged. staceyw's implementation for Events is fine too as far as I can tell.


    Observable is implemented correctly by staceyw.

    All others are wrong regardless of whether they give the correct output.

    staceyw's implementation of Observable breaks down as soon as more than one observer subscribes to LengthChanged. The state of the (shared) lastLen variable is modified when the first subscriber receives the next string. So depending on evaluation order other subscribers may not see the change as for them the value is filtered out in the Where expression. jyates revised solution on the other hand:


     return TextChanged.Select(s => s == null ? 0 : s.Length).DistinctUntilChanged();     

     looks perfect imho.