Pull Versus Push

The IObservable<T> and IObserver<T> interfaces might look a little weird at first, but they are actually very closely related to the well-known System.Collections.Generic interfaces IEnumerable<T> and IEnumerator<T>. In fact, they are mathematical duals. It’s normal you perceive this as rather abstract, but it’s rather simple. Follow me.

From our discussion about statements, recall that IEnumerable<T>’s primary use is the foreach loop statement. Collections implement this interface, allowing us to retrieve their data by pulling it out one by one. In fact, the consumer is in control, which makes this an interactive mechanism. To pull data from the source, the consumer asks the enumerator for more data by calling MoveNext and ...

Get C# 5.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.