Table of Contents

Class DistinctOperator

Namespace
RxSharp.Operators
Assembly
RxSharp.dll

Implements the Distinct operator. Mirrors rxjs's distinct.

public static class DistinctOperator
Inheritance
System.Object
DistinctOperator

Methods

Distinct<T>(Observable<T>, IEqualityComparer<T>?)

Emits a value from source only if it has not been seen before, anywhere in the stream since subscription — using comparer (or the default equality comparer for T).

public static Observable<T> Distinct<T>(this Observable<T> source, IEqualityComparer<T>? comparer = null)

Parameters

source Observable<T>

The source observable to filter for distinct values.

comparer IEqualityComparer<T>?

The comparer used to test equality. Defaults to EqualityComparer<T>.Default when null.

Returns

Observable<T>

An observable that emits only values from source not previously emitted.

Type Parameters

T

The element type of the source observable.

Remarks

Unlike DistinctUntilChanged<T>(Observable<T>, IEqualityComparer<T>?), which only compares a value against the immediately preceding one, this operator tracks every distinct value ever seen (in an internal set) for the lifetime of the subscription — so a repeated value is suppressed no matter how long ago it last appeared.

Distinct<T, TKey>(Observable<T>, Func<T, TKey>, IEqualityComparer<TKey>?)

Emits a value from source only if the key computed for it by keySelector has not been computed before, anywhere in the stream since subscription — using comparer (or the default equality comparer for TKey).

public static Observable<T> Distinct<T, TKey>(this Observable<T> source, Func<T, TKey> keySelector, IEqualityComparer<TKey>? comparer = null)

Parameters

source Observable<T>

The source observable to filter for distinct keys.

keySelector Func<T, TKey>

A function that extracts the comparison key from each value.

comparer IEqualityComparer<TKey>?

The comparer used to test key equality. Defaults to EqualityComparer<TKey>.Default when null.

Returns

Observable<T>

An observable that emits only values from source whose key was not previously seen.

Type Parameters

T

The element type of the source observable.

TKey

The type of the key extracted from each value for distinctness tracking.

Remarks

The original value (not the key) is what gets emitted. If keySelector throws, the exception is forwarded via OnError and the source subscription is torn down.