Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
IObservable(T) Interface
Collapse the table of content
Expand the table of content

IObservable<T> Interface

.NET Framework 4.6 and 4.5

Defines a provider for push-based notification.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

generic<typename T>
public interface class IObservable

Type Parameters

out T

The object that provides notification information.

This type parameter is covariant. That is, you can use either the type you specified or any type that is more derived. For more information about covariance and contravariance, see Covariance and Contravariance in Generics.

The IObservable<T> type exposes the following members.

Public methodSupported by Portable Class LibrarySubscribeNotifies the provider that an observer is to receive notifications.

The IObserver<T> and IObservable<T> interfaces provide a generalized mechanism for push-based notification, also known as the observer design pattern. The IObservable<T> interface represents the class that sends notifications (the provider); the IObserver<T> interface represents the class that receives them (the observer). T represents the class that provides the notification information. In some push-based notifications, the IObserver<T> implementation and T can represent the same type.

The provider must implement a single method, Subscribe, that indicates that an observer wants to receive push-based notifications. Callers to the method pass an instance of the observer. The method returns an IDisposable implementation that enables observers to cancel notifications at any time before the provider has stopped sending them.

At any given time, a given provider may have zero, one, or multiple observers. The provider is responsible for storing references to observers and ensuring that they are valid before it sends notifications. The IObservable<T> interface does not make any assumptions about the number of observers or the order in which notifications are sent.

The provider sends the following three kinds of notifications to the observer by calling IObserver<T> methods:

  • The current data. The provider can call the IObserver<T>::OnNext method to pass the observer a T object that has current data, changed data, or fresh data.

  • An error condition. The provider can call the IObserver<T>::OnError method to notify the observer that some error condition has occurred.

  • No further data. The provider can call the IObserver<T>::OnCompleted method to notify the observer that it has finished sending notifications.

The following example illustrates the observer design pattern. It defines a Location class that contains latitude and longitude information.

No code example is currently available or this language may not be supported.

The LocationTracker class provides the IObservable<T> implementation. Its TrackLocation method is passed a nullable Location object that contains the latitude and longitude data. If the Location value is not nullptr, the TrackLocation method calls the OnNext method of each observer.

No code example is currently available or this language may not be supported.

If the Location value is nullptr, the TrackLocation method instantiates a LocationNotFoundException object, which is shown in the following example. It then calls each observer's OnError method and passes it the LocationNotFoundException object. Note that LocationNotFoundException derives from Exception, but does not add any new members.

No code example is currently available or this language may not be supported.

Observers register to receive notifications from a TrackLocation object by calling its IObservable<T>::Subscribe method, which assigns a reference to the observer object to a private generic List<T> object. The method returns an Unsubscriber object, which is an IDisposable implementation that enables observers to stop receiving notifications. The LocationTracker class also includes an EndTransmission method. When no further location data is available, the method calls each observer's OnCompleted method and then clears the internal list of observers.

In this example, the LocationReporter class provides the IObserver<T> implementation. It displays information about the current location to the console. Its constructor includes a name parameter, which enables the LocationReporter instance to identify itself in its string output. It also includes a Subscribe method, which wraps a call to the provider's Subscribe method. This allows the method to assign the returned IDisposable reference to a private variable. The LocationReporter class also includes an Unsubscribe method, which calls the IDisposable::Dispose method of the object that is returned by the IObservable<T>::Subscribe method. The following code defines the LocationReporter class.

No code example is currently available or this language may not be supported.

The following code then instantiates the provider and the observer.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library
© 2015 Microsoft