// ReSharper disable once UnusedParameter.Local private void Dispose(bool disposing) { // 1. lock(this) is bad // 2. sync object just for subscribe/dispose will add 8 bytes to obj size, rarely used // 3. reusing _container._locker could interfere with data writes, // but in steady state there should be no subscriptions/disposals. _container.AcquireLock(); try { DoDispose(); } finally { _container.ReleaseLock(); } void DoDispose() { if (ReferenceEquals(_container._subscriptions, this)) { _container._subscriptions = null; return; } // single -> [] is one-way, we have read existing via if (_container._subscriptions is ContainerSubscription[] subsArray) { for (int i = 0; i < subsArray.Length; i++) { if (ReferenceEquals(subsArray[i], this)) { Volatile.Write(ref subsArray[i], null); break; } } } } }