public Subscription(BehaviorSubject <T> parent, IObserver <T> unsubscribeTarget)
 {
     this.parent            = parent;
     this.unsubscribeTarget = unsubscribeTarget;
 }
        public IDisposable Subscribe(IObserver <T> observer)
        {
            if (observer == null)
            {
                throw new ArgumentNullException("observer");
            }
            Exception ex    = null;
            T         value = default(T);

            BehaviorSubject <T> .Subscription subscription = null;
            object obj = this.observerLock;

            lock (obj)
            {
                this.ThrowIfDisposed();
                if (!this.isStopped)
                {
                    ListObserver <T> listObserver = this.outObserver as ListObserver <T>;
                    if (listObserver != null)
                    {
                        this.outObserver = listObserver.Add(observer);
                    }
                    else
                    {
                        IObserver <T> observer2 = this.outObserver;
                        if (observer2 is EmptyObserver <T> )
                        {
                            this.outObserver = observer;
                        }
                        else
                        {
                            this.outObserver = new ListObserver <T>(new ImmutableList <IObserver <T> >(new IObserver <T>[]
                            {
                                observer2,
                                observer
                            }));
                        }
                    }
                    value        = this.lastValue;
                    subscription = new BehaviorSubject <T> .Subscription(this, observer);
                }
                else
                {
                    ex = this.lastError;
                }
            }
            if (subscription != null)
            {
                observer.OnNext(value);
                return(subscription);
            }
            if (ex != null)
            {
                observer.OnError(ex);
            }
            else
            {
                observer.OnCompleted();
            }
            return(Disposable.Empty);
        }