First and last

The next figure shows how to create a sequence by emitting only the first element emitted by a source Observable sequence:

First and last

The first() and last() methods are quite easy to figure out. They emit only the first or last element emitted by Observable. Both of them can even get Func1 as a parameter: a predicate that can be used to decide the actual first or last element we are interested in.

The next figure shows how last() can be applied to a completed sequence to create a new Observable that emits only the last emitted element:

First and last

Both first() and last() come with a similar variant: firstOrDefault() and lastOrDefault(). These two functions come in handy when the Observable sequence completes without emitting any value. In this scenario, we can decide a default value to emit if the Observable didn't emit any.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.17.140