user25064 user25064 - 1 month ago 9x
C# Question

Get latest message from an IObservable

I have a very simple task that I would like to accomplish with rx and I am not sure how to accomplish it. Consider the code below as minimum example.

var subject = new Subject<int>();


Assert.AreEqual(2, subject.__________());
// > what goes above <

The method should be something exposed only to the
interface. There are two useful looking candidates that do not do what I hope. The
method will block until the next message which is not the behavior that I am looking for. The
documentation looked very promising but failed to deliver what I hoped for.


There's no single method that will do it. Observables need a subscription, which can also take the form of an operator, to propagate values. You need that subscription active when the subject emits (or OnNexts) the values. There's nothing that caches or stores the values for you.

Here's an example using a raw subscription:

var subject = new Subject<int>();

int last = 0;

using (subject.Subscribe(i => last = i))
Assert.AreEqual(2, last);