Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Rx Workshop: Observables versus Events
Nov 12, 2011 at 4:16 PMHere 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; m_LengthChanged(length); } }; } public virtual void OnTextChanged(string text) { var t = TextChanged; if (t != null) t(text); } public event Action<int> LengthChanged { add { m_LengthChanged += value; } remove { m_LengthChanged -= value; } } }Rx Workshop: Observables versus Events
Nov 12, 2011 at 3:53 PMKurator'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.
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:
looks perfect imho.