+4  A: 

In general, calling the delegates individually gives you more control over the behavior:

  • If one delegate raises an exception you can keep calling the others, for example, or remove the faulted delegate from your list.
  • If you want to call the delegates in parallel, it's really easy.
  • If you need to call them in a certain order, you can easily guarantee the correct order (I'm not sure that the order of multicast delegate calls is defined).
Gabe
In addition, you would also run into problems with duplicates if you used the multicast delegate approach. If two observers with the exact same OnNext methods were added, and one then removed, that would remove the callback for both of them.
Freed
Freed: No, that's not a problem. Only the first instance of a given delegate is removed.
Gabe
@Gabe - You're right. What will happen however is that the "wrong" delegate will be removed which would change the order the observers are notified (which may or may not be relevant).
Freed
The problem with the benefits you describe is that utilising any of them would make your observable act differently than the rest of Rx.
Richard Szalay
+2  A: 

Usually you don't implement IObservable<T> yourself, you return an IObservable<T> from a method using one of the generation methods (like Observable.Create).

However, if you are going to implement the interface yourself, you should wrap an internal Subject<T> which will handle all the concurrency issues for you:

public class CustomObservable<T> : IObservable<T>
{
    private Subject<T> subject = new Subject<T>();

    public IDisposable Subscribe(IObserver<T> observer)
    {
        return subject.Subscribe(observer);
    }

    private void EmitValue(T value)
    {
        subject.OnNext(value);
    }
}

NB: If you decide to stick with the delegate (for whatever reason), at least make sure you are unsubscribing in your IDisposable return value:

observers += observer.OnNext;
return Disposable.Create(() => observers -= observer.OnNext);
Richard Szalay
@Richard, thanks for answering. Note however that my library is not based on Rx, I am relying only on the two interfaces in the .NET 4.0 BCL. I only checked with the Rx implementation to see how it's usually done.
stakx
Yes, this is great advice! Think of implementing IObservable directly as common as implementing IEnumerable; usually you'll try to use one of the already-built IObservable implementations like Subject<T>
Paul Betts