Table of Contents

Class Subject<T>

Namespace
RxSharp.Subjects
Assembly
RxSharp.dll

A multicast IObservable<T> that is also an IObserver<T>: every value/error/completion pushed into it via the IObserver<T> methods is forwarded to every observer currently subscribed at that moment. Unlike Observable<T>, it is implemented directly against the BCL interfaces rather than by subclassing Observable<T> — same shape as rxjs's own Subject.

public class Subject<T> : IObservable<T>, IObserver<T>, IDisposable

Type Parameters

T

The type of values pushed through the subject.

Inheritance
System.Object
Subject<T>
Implements
IObserver<T>
IDisposable
Derived

Properties

HasError

Gets a value indicating whether the subject has already terminated via OnError(Exception). Exposed to subclasses (see BehaviorSubject<T>, AsyncSubject<T>) so they can consult the error state for their own bookkeeping.

protected bool HasError { get; }

Property Value

bool

IsDisposed

Gets a value indicating whether Dispose() has been called on this subject.

public bool IsDisposed { get; }

Property Value

bool

IsStopped

Gets a value indicating whether the subject has already terminated via OnError(Exception) or OnCompleted(). Exposed to subclasses (see ReplaySubject<T>) so they can consult the terminal state for their own bookkeeping — e.g. to avoid buffering a value that arrives after termination, even though it is correctly no longer forwarded to subscribers.

protected bool IsStopped { get; }

Property Value

bool

ThrownError

Gets the error the subject terminated with, or null if it hasn't errored (or was disposed after erroring).

protected Exception? ThrownError { get; }

Property Value

Exception?

Methods

AsObservable()

Wraps this subject so it is exposed to callers purely as an Observable<T>, hiding its IObserver<T> (push) side.

public Observable<T> AsObservable()

Returns

Observable<T>

An Observable<T> that subscribes through this subject.

Dispose()

Disposes the subject: marks it disposed and drops all current observers without notifying them.

public virtual void Dispose()

OnCompleted()

Terminates the subject normally: forwards completion to every observer currently subscribed, marks the subject as stopped, and clears the observer list (any late subscriber will immediately receive completion too — see Subscribe(IObserver<T>)). A no-op once the subject has already stopped or been disposed.

public virtual void OnCompleted()

OnError(Exception)

Terminates the subject with an error: forwards it to every observer currently subscribed, marks the subject as stopped, and clears the observer list (any late subscriber will immediately receive the same error — see Subscribe(IObserver<T>)). A no-op once the subject has already stopped or been disposed.

public virtual void OnError(Exception error)

Parameters

error Exception

The terminating error.

OnNext(T)

Pushes a value to every observer currently subscribed. A no-op once the subject has stopped or been disposed.

public virtual void OnNext(T value)

Parameters

value T

The value to push.

RemoveObserver(IObserver<T>)

Removes a previously subscribed observer from the notification list. Used by the disposable returned from Subscribe(IObserver<T>).

protected void RemoveObserver(IObserver<T> observer)

Parameters

observer IObserver<T>

The observer to remove.

Subscribe(IObserver<T>)

Subscribes observer to future values pushed through this subject. If the subject has already terminated (OnError(Exception) or OnCompleted() already called), the observer immediately receives that same terminal notification instead of being added to the observer list.

public virtual IDisposable Subscribe(IObserver<T> observer)

Parameters

observer IObserver<T>

The observer to subscribe.

Returns

IDisposable

A disposable that removes observer from the subject's observer list when disposed, or Empty if the subject had already terminated (nothing to unsubscribe from).

Subscribe(Action<T>?, Action<Exception>?, Action?)

Delegate-based convenience overload of Subscribe(IObserver<T>), mirroring the same overload on Observable<T>. Lets a caller pass bare lambdas/method groups directly to a Subject<T> instance without first wrapping it via AsObservable(), matching what real rxjs subjects support directly.

public IDisposable Subscribe(Action<T>? onNext = null, Action<Exception>? onError = null, Action? onComplete = null)

Parameters

onNext Action<T>?

Called for each value pushed through the subject. Optional.

onError Action<Exception>?

Called once if the subject terminates with an error. Optional.

onComplete Action?

Called once if the subject terminates normally. Optional.

Returns

IDisposable

A disposable that unsubscribes the constructed observer, per Subscribe(IObserver<T>).