concept Subscribe in category .net

This is an excerpt from Manning's book Rx.NET in Action.
The IObservable<T> interface has only one method, Subscribe, that allows observers to be subscribed for notifications. The Subscribe method returns an IDisposable object that represents the subscription and allows the observer to unsubscribe at any time by calling the Dispose method. Observables hold the collection of subscribed observers and notify them when there’s something worth notifying. This is done using the IObserver<T> interface, which also has resided in the System namespace since version 4.0 of the .NET Framework, as shown here.
By far, the most desirable way to create an observer is doing it without leaving your pipeline so that everything is centralized and, thus, easier to read and maintain. Luckily, it’s also the easiest and most straightforward way of creating an observer, as shown in figure 6.3. All it takes is using one of the overloads of the Subscribe extension method that resides under the static ObservableExtensions class, which is under the System namespace.
Figure 6.3. Creating the observer and subscribing it as part of the pipeline with the Subscribe operator
![]()
Here’s an example that creates and subscribes an observer that prints to the screen (much like the ConsoleObserver created in chapter 4). In this case, you pass all the observer’s functions as arguments to the Subscribe operator:
![]()
In the example, I provide lambda expressions as arguments to the Subscribe method. This results in an observer that prints to the screen every notification it receives.