Table of Contents

Class LastOrDefault

Namespace
Bonsai.Reactive
Assembly
Bonsai.Core.dll

Represents an operator that returns the last element of an observable sequence, or a default value if no such element exists.

Marble diagram

If the sequence has no elements, LastOrDefault will emit a default value before terminating successfully.

Tip

LastOrDefault is one of the simplest and most efficient ways of capturing the end of an observable sequence explicitly as a notification in the workflow. You can use it to react to the termination of a sequence regardless of whether that sequence produces values.

Example

Use LastOrDefault to retrieve the last element of a sequence or a default value if the sequence is empty.

LastOrDefault Example

Alternative

Use TakeLast to retrieve one or more elements from the end of the sequence.

This is a subtle but important difference between the LastOrDefault operator and TakeLast(1), which will also complete successfully when the source sequence is empty, but will not emit any values.

public class LastOrDefault : Combinator
Inheritance
LastOrDefault
Inherited Members

Methods

Process<TSource>(IObservable<TSource>)

Returns the last element of an observable sequence, or a default value if no such element exists.