Why would you ever subscribe to or even create an observable that never yields anything, not even an error, nor completion?
3 Answers
It is a fundamental "atom" in the composition of observable event streams and comes up in a variety of situations - but one of the most common uses for Observable.Never
in practice is in writing tests. It easily enables robust checking of timeout logic, for example - and is often combined with the TestScheduler
or other virtual time schedulers in test scenarios. If you examine the source code of Rx itself, you find it appears in many unit tests.

- 29,019
- 9
- 86
- 120
I've seen it used with Observable.Window and Observable.Buffer for the closing observable. This is a trivial example as the real code is too complex to post.
static IObservable<Unit> CloseWindow(int interval)
{
if (interval <= 0)
{
return Observable.Never<Unit>();
}
return Observable.Interval(TimeSpan.FromSeconds(interval)).Select(_ => Unit.Default);
}
Use when grouping data for analyst and sometimes it requires all the data and sometimes not. Again trivial example
static IObservable<double> AverageOfWindow(IObservable<int> source, int interval)
{
return source.Buffer(CloseWindow(interval)).Select(o => o.Average());
}
If using Empty it would never produce a result and by using Never it's basically by passing the Buffer and Window methods. Could write the observable to skip the Buffer when it's not needed but easier and less code to maintain if the CloseWindow has all the logic to determine when the close the buffer sequence. Again in this example it's trivial but in real code the logic to decide when the buffer closes is more complex.

- 3,219
- 1
- 16
- 25
-
Thank you very much. These are the very overloads of `Buffer` that I do not understand. I couldn't find any explanation for them anywhere. Could you please explain them? I have posted a separate question for them here: http://stackoverflow.com/q/37824033/303685 – Water Cooler v2 Jun 14 '16 at 23:58
I have considered this myself. The conclusion I came to is that frameworks that you take a dependency on would use this (or you would use this if you are creating a framework). I see it as the equivalent of an empty IEnumerable
. Consider the case where you call a method that returns an IEnumerable
. Perhaps the IEnumerable
that is returned from the method depends on the parameters passed to the method; there may be no data to be returned in some cases. Some... people (I struggled with a few choice words here) might return null
but a more enlightened developer would return an empty IEnumerable
. I see an observable that never yields anything as being equivalent to an empty IEnumerable
. Of course, you could make the argument that Observable.Empty
(which returns an observable that completes immediately) is really the equivalent of an empty IEnumerable
but I think it probably depends on the situation. Whoever is designing the framework would have to decide which is semantically correct, Observable.Empty
or Observable.Never
.
That's my two cents. Perhaps somebody will have a better reason.

- 6,839
- 4
- 29
- 47
-
Nice thinking. One use I have seen of it is on this page (http://rxwiki.wikidot.com/101samples#toc39) where it is used like a `Missing` argument that you used to pass to COM servers when doing OLE automation like from VBA. In other words, when someone wants you to pass an `IObservable
` and you want to say, "I don't care, I won't give you one because I don't have one and I really don't care whether or not you get one at that place." This correlates somewhat with your commentary, and it also draws a fine line between `Never` and `Empty`. – Water Cooler v2 Jun 14 '16 at 06:35