dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'array')

Error message

Value cannot be null. (Parameter 'array')

What it means

CompositeDisposable.CopyTo(IDisposable[] array, int arrayIndex) throws ArgumentNullException when the target array is null. CopyTo implements ICollection<IDisposable> and needs a valid destination array; a null array is rejected immediately before any locking or copying occurs.

Solutions

  1. Allocate the array before copying: var snapshot = new IDisposable[composite.Count]; composite.CopyTo(snapshot, 0);
  2. Prefer composite.ToArray() (LINQ) which handles allocation internally.
  3. Null-check the destination array before calling CopyTo.

Example fix

// before
IDisposable[] snapshot = GetBuffer(); // may be null
composite.CopyTo(snapshot, 0);
// after
var snapshot = new IDisposable[composite.Count];
composite.CopyTo(snapshot, 0);
Defensive patterns

Strategy: validation

Validate before calling

var arr = destination ?? new IDisposable[composite.Count];
composite.CopyTo(arr, 0);

Type guard

bool CanCopy(CompositeDisposable c, IDisposable[] a) => c != null && a != null;

Try / catch

try { composite.CopyTo(array, index); } catch (ArgumentNullException ex) when (ex.ParamName == "array") { array = new IDisposable[composite.Count]; composite.CopyTo(array, index); }

Prevention

When it happens

Trigger: Calling composite.CopyTo(null, 0), typically when the destination array is produced by a method that returned null or a field that was never allocated.

Common situations: Snapshotting the composite's contents for diagnostics/logging where the buffer array is conditionally allocated; passing the result of a factory function that can return null.

Related errors


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/cdba2f75e26829d9. Report an issue: GitHub.

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Disposables/CompositeDisposable.cs:427

                var current = _disposables;
                return current is List<IDisposable?> list
                    ? list.Contains(item)
                    : ((Dictionary<IDisposable, int>) current).ContainsKey(item);
            }
        }

        /// <summary>
        /// Copies the disposables contained in the <see cref="CompositeDisposable"/> to an array, starting at a particular array index.
        /// </summary>
        /// <param name="array">Array to copy the contained disposables to.</param>
        /// <param name="arrayIndex">Target index at which to copy the first disposable of the group.</param>
        /// <exception cref="ArgumentNullException"><paramref name="array"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="arrayIndex"/> is less than zero. -or - <paramref name="arrayIndex"/> is larger than or equal to the array length.</exception>
        public void CopyTo(IDisposable[] array, int arrayIndex)
        {
            if (array == null)
            {
                throw new ArgumentNullException(nameof(array));
            }

            if (arrayIndex < 0 || arrayIndex >= array.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(arrayIndex));
            }

            lock (_gate)
            {
                // disposed composites are always empty
                if (_disposed)
                {
                    return;
                }

                if (arrayIndex + _count > array.Length)
                {
                    // there is not enough space beyond arrayIndex 

View on GitHub (pinned to 94b5d5ab91)