Class BehaviorSubject<T>
Represents a value that changes over time. Observers can subscribe to the subject to receive the last (or initial) value and all subsequent notifications.
Inherited Members
Namespace: System.Reactive.Subjects
Assembly: System.Reactive.Linq.dll
Syntax
public sealed class BehaviorSubject<T> : ISubject<T>, ISubject<T, T>, IObserver<T>, IObservable<T>, IDisposable
Type Parameters
| Name | Description |
|---|---|
| T | The type of the elements processed by the subject. |
Constructors
| Improve this Doc View SourceBehaviorSubject(T)
Initializes a new instance of the BehaviorSubject<T> class which creates a subject that caches its last value and starts with the specified value.
Declaration
public BehaviorSubject(T value)
Parameters
| Type | Name | Description |
|---|---|---|
| T | value | Initial value sent to observers when no other value has been received by the subject yet. |
Properties
| Improve this Doc View SourceHasObservers
Indicates whether the subject has observers subscribed to it.
Declaration
public bool HasObservers { get; }
Property Value
| Type | Description |
|---|---|
| Boolean |
Value
Gets the current value or throws an exception.
Declaration
public T Value { get; }
Property Value
| Type | Description |
|---|---|
| T | The initial value passed to the constructor until OnNext(T) is called; after which, the last value passed to OnNext(T). |
Remarks
Value is frozen after OnCompleted() is called.
After OnError(Exception) is called, Value always throws the specified exception.
An exception is always thrown after Dispose() is called.
Exceptions
| Type | Condition |
|---|---|
| ObjectDisposedException | Dispose was called. |
Methods
| Improve this Doc View SourceDispose()
Unsubscribe all observers and release resources.
Declaration
public void Dispose()
OnCompleted()
Notifies all subscribed observers about the end of the sequence.
Declaration
public void OnCompleted()
OnError(Exception)
Notifies all subscribed observers about the exception.
Declaration
public void OnError(Exception error)
Parameters
| Type | Name | Description |
|---|---|---|
| Exception | error | The exception to send to all observers. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
OnNext(T)
Notifies all subscribed observers about the arrival of the specified element in the sequence.
Declaration
public void OnNext(T value)
Parameters
| Type | Name | Description |
|---|---|---|
| T | value | The value to send to all observers. |
Subscribe(IObserver<T>)
Subscribes an observer to the subject.
Declaration
public IDisposable Subscribe(IObserver<T> observer)
Parameters
| Type | Name | Description |
|---|---|---|
| IObserver<T> | observer | Observer to subscribe to the subject. |
Returns
| Type | Description |
|---|---|
| IDisposable | Disposable object that can be used to unsubscribe the observer from the subject. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|