Export (0) Print
Expand All

IObserver(T) Interface

Provides a mechanism for receiving push-based notifications.

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

type IObserver<'T> =  interface end

Type Parameters

in T

The object that provides notification information.

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

The IObserver(T) type exposes the following members.

  NameDescription
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsOnCompletedNotifies the observer that the provider has finished sending push-based notifications.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsOnErrorNotifies the observer that the provider has experienced an error condition.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsOnNextProvides the observer with new data.
Top

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.

An IObserver(T) implementation arranges to receive notifications from a provider (an IObservable(T) implementation) by passing an instance of itself to the provider's IObservable(T).Subscribe method. This method returns an IDisposable object that can be used to unsubscribe the observer before the provider finishes sending notifications.

The IObserver(T) interface defines the following three methods that the observer must implement:

  • The OnNext method, which is typically called by the provider to supply the observer with new data or state information.

  • The OnError method, which is typically called by the provider to indicate that data is unavailable, inaccessible, or corrupted, or that the provider has experienced some other error condition.

  • The OnCompleted method, which is typically called by the provider to indicate that it has finished sending notifications to observers.

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 LocationReporter class provides the IObserver(T) implementation. It displays information about the current location to the console. Its constructor includes a name parameter, which allows 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 enables 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 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 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 a null reference (Nothing in Visual Basic), 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 a null reference (Nothing in Visual Basic), 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.

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.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

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

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft