Observer pattern implementation in C# In order to implement the Observer pattern in C#, one of the ways to go (at least, the one I chose) is to make classes that implement the IObservable&lt;T&gt; for the observable objects and the IObserver&lt;T&gt; for the observers.

In a project of mine I created a base class from which every observable inherits:

public class Observable&lt;T&gt; : IObservable&lt;T&gt;
{
    private SubscriptionManager&lt;T&gt; _subscriptionManager;

    public Observable()
    {
        _subscriptionManager = new SubscriptionManager&lt;T&gt;(
            new List&lt;IObserver&lt;T&gt;&gt;());
    }

    public IDisposable Subscribe(IObserver&lt;T&gt; observer)
    {
        _subscriptionManager.Subscribe(observer);

        return _subscriptionManager;
    }

    public void Notify(T obj)
    {
        _subscriptionManager.Notify(obj);
    }
}


and an IDisposable class that manages the subscriptions to the observable object:

public class SubscriptionManager&lt;T&gt; : IDisposable
{
    private ICollection&lt;IObserver&lt;T&gt;&gt; _observers;
    private IObserver&lt;T&gt; _observer;

    public SubscriptionManager(ICollection&lt;IObserver&lt;T&gt;&gt; observers)
    {
        if (observers == null)
        {
            throw new ArgumentNullException("observers");
        }

        _observers = observers;
    }

    public void Subscribe(IObserver&lt;T&gt; observer)
    {
        _observers.Add(observer);
        _observer = observer;
    }

    public void Notify(T obj)
    {
        foreach (var observer in _observers)
        {
            observer.OnNext(obj);
        }
    }

    public void Dispose()
    {
        _observers.Remove(_observer);
    }
}


Do you see anything that can be done more efficiently, or in a better way? Do you see anything else that may be wrong?
Here's a problem:

var o = new Observable&lt;Unit&gt;();
var a = o.Subscribe(_ =&gt; Console.WriteLine("A"));
var b = o.Subscribe(_ =&gt; Console.WriteLine("B"));
a.Dispose();
o.Notify(Unit.Default);


This prints


A


Disposing of a removed the wrong observer.



I'd recommend reading Why shouldn't I implement IObservable&lt;T&gt;?


  The reason you shouldn't implement IObservable&lt;T&gt; is the same reason
  you don't usually implement IEnumerable&lt;T&gt;, is that somebody has most
  likely already built the thing you want.