Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the key difference between Subject<T> and ReplaySubject<T>?

What is the difference between System.Reactive.Subjects.Subject<T> and System.Reactive.Subjects.ReplaySubject<T> classes?

One doesn't derive from another, but they have the same description and implement same interfaces in MSDN.

like image 421
enkryptor Avatar asked Jan 25 '26 10:01

enkryptor


1 Answers

Take this code:

var subject = new Subject<int>();

subject.OnNext(42);
subject.OnCompleted();

subject.Subscribe(x => Console.WriteLine(x));

And compare to this:

var subject = new ReplaySubject<int>();

subject.OnNext(42);
subject.OnCompleted();

subject.Subscribe(x => Console.WriteLine(x));

The first produces no values. The second produces 42.

Basically Subject only produces values to current subscribers and ReplaySubject remembers values for future subscribes (when it "replays" the values).

like image 94
Enigmativity Avatar answered Jan 28 '26 00:01

Enigmativity