Class DistinctOperator
Implements the Distinct operator. Mirrors rxjs's distinct.
public static class DistinctOperator
- Inheritance
-
System.ObjectDistinctOperator
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
sourceObservable<T>The source observable to filter for distinct values.
comparerIEqualityComparer<T>?The comparer used to test equality. Defaults to EqualityComparer<T>.Default when null.
Returns
- Observable<T>
An observable that emits only values from
sourcenot previously emitted.
Type Parameters
TThe 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
sourceObservable<T>The source observable to filter for distinct keys.
keySelectorFunc<T, TKey>A function that extracts the comparison key from each value.
comparerIEqualityComparer<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
sourcewhose key was not previously seen.
Type Parameters
TThe element type of the source observable.
TKeyThe 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.