Table of Contents

Class FinalizeOperator

Namespace
RxSharp.Operators
Assembly
RxSharp.dll

Extension methods implementing the finalize operator.

public static class FinalizeOperator
Inheritance
System.Object
FinalizeOperator

Methods

Finalize<T>(Observable<T>, Action)

Mirrors source exactly, but runs callback exactly once when the resulting observable terminates — whether that's because source completed, errored, or the subscriber unsubscribed early. This is rxjs's try/finally equivalent for observables.

public static Observable<T> Finalize<T>(this Observable<T> source, Action callback)

Parameters

source Observable<T>

The source sequence to finalize.

callback Action

The teardown logic to run exactly once, on completion, error, or unsubscription.

Returns

Observable<T>

An observable that mirrors source, invoking callback on termination.

Type Parameters

T

The type of values emitted by source.

Remarks

Deliberately does not create any intermediate Subscriber<T>: source is subscribed with the exact downstream Subscriber<T> this operator receives, matching rxjs's own finalize.ts (source.subscribe(subscriber)) almost verbatim. That is what makes this operator's disposal behavior automatically correct with zero extra bookkeeping: because there is no wrapper subscriber in between, a downstream disposal (e.g. an early-completing Take further down the chain) is visible to source immediately, even for a fully-synchronous, self-checking source — there is nothing here that could introduce the disposal-cascade gap described in CLAUDE.md's Learnings, since this operator never creates the kind of intermediate subscriber that gap is about. callback is registered as a teardown via Add(Action) only after source's own subscribe call returns; if the subscriber is already stopped by then (completed, errored, or synchronously unsubscribed), Add(Action) runs it immediately instead of deferring it, which is what lets callback observe termination that already happened synchronously during subscribe.