user3685285
user3685285

Reputation: 6626

IObservable.Add vs. IObservable.Subscribe

What is the difference between these two methods, and what is the best situation for each? I know they are both able to attach a function for handling emissions from an IObservable, but I don't really understand the differences beyond that.

EDIT

Sorry, I should have specified. The definition of IObservable.Add is here:

https://msdn.microsoft.com/en-us/library/ee370414.aspx

Maybe it's just an F# thing. I'm using F# by the way. Not C#.

Upvotes: 5

Views: 673

Answers (2)

RMills330
RMills330

Reputation: 329

Appreciating I'm a bit late to the party...

In the context of FSharp.Control.Observable:

  • add does indeed subscribe to the observable, returning unit.
  • subscribe is as above, but instead returns an IDisposable that can be used to unsubscribe.

Source: From FSharp.Core documentation

Upvotes: 2

James World
James World

Reputation: 29806

There is no extension method IObservable.Add in the Rx library, and this method is not part of the interface - so I am not sure where you got that from. IObservable.Subscribe is the means by which an observer subscribes for notifications from an Observable stream.

You can see the full definitions of these interfaces (which are part of the .NET Base Class Library from version 4.0) here:

You may wish to look at the resources for Rx on the msdn page here - especially the videos, since it appears you are at the start of your Rx journey.

Edit

In light of the clarification that you are using F# - yes it appears Add is F# specific, as well as the Subscribe extension method you are referring too; I've never used it in C#. Both these F# methods appear to only react to OnNext notifications and as such seem rather odd. I would steer clear of these methods and stick to Rx.NET if you want to use observables. (Rx is usable from F#).

Upvotes: 0

Related Questions