automatonic automatonic - 29 days ago 14
C# Question

How to create an observable that produces a single value and never completes

I am aware of

Observable.Never()
as a way to create a sequence that never completes, but is there an extension/clean process for creating an observable that produces a single value and then never completes? Do i go with
Observable.Create(...)
?
Observable.Concat(Observable.Return(onlyValue), Observable.Never<T>())
? Or is there something built in or more "RXy" than this?

Answer

For your specific question, a simple choice is to use ‛Never‛ and ‛StartWith‛:

Observable.Never<int>().StartWith(5)

But for the more general case of "I have an observable sequence that will produce some results and eventually complete and I want to change it so it does not complete" (of which your question is a special case), your Concat idea is the way to do it

source.Concat(Observable.Never<int>());

or

Observable.Concat(source, Observable.Never<int>());