2

Given a sequence, IObservable<int> source;, is there a difference between:

var published = source.Publish(0);
var publishedConnection = published.Connect();

and

var replayed = source.StartWith(0).Replay(1);
var replayedConnection = replayed.Connect();

As far as I'm aware, they're very similar. They both have a default value of zero, on subscription, the observer will immediately receive the last value from source, and all further values from source are pushed out to the subscriber.

I have a vague inkling that I read somewhere (which I can't find now) that if source were to complete, published would not pass any value to new subscribers, but instead complete immediately, whereas replayed would still replay the last value to new subscribers before completing.

Have I remembered this correctly (and can anyone find a source stating so), and are there any other differences between these two methods?

Philip C
  • 1,819
  • 28
  • 50

1 Answers1

2

The difference you have listed is accurate. Replay replays whereas Publish just publishes. I checked this by writing five lines of code. There are no other significant differences. The source code is also available at http://rx.codeplex.com - although I admit it's not readily decipherable to those unfamiliar with it.

Code:

var source = Observable.Return(1);

// comment out as appropriate:
// this gives no output
var conn = source.Publish(0);
// this gives 1
var conn = source.StartWith(0).Replay(1);

conn.Connect();

conn.Subscribe(Console.WriteLine);
James World
  • 29,019
  • 9
  • 86
  • 120
  • 1
    Although a technical detail as you know, it may be worth noting that the primary difference between these operators is their purpose: The particular overload of `Publish` in the question is designed to hide a `BehaviorSubject` and `Replay` is designed to hide a `ReplaySubject`. – Dave Sexton Dec 05 '14 at 15:40