5

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#.

user3685285
  • 6,066
  • 13
  • 54
  • 95

2 Answers2

2

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

RMills330
  • 319
  • 1
  • 12
0

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#).

Community
  • 1
  • 1
James World
  • 29,019
  • 9
  • 86
  • 120
  • I think maybe you've misunderstood the question. The OP is referring to the `Control.Observable` F# module, not `IObservable` or Rx.NET. The answer was well-explained, just aimed at the wrong thing, IMO. – Ben Collins Jun 06 '17 at 21:00
  • (-1) I don't agree with the suggestion to stay clear of those methods... Although the labelling (possibly) isn't the clearest, they do have a useful purpose and frequently come up in various reading material covering observables in the context of F#. Furthermore, this doesn't get straight to the answer. – RMills330 Jul 16 '22 at 08:39