dotnet/reactive · error · ArgumentException

Disposables collection can not contain null values…

Error message

Disposables collection can not contain null values. (Parameter 'disposables')

What it means

When CompositeDisposable builds a dictionary of distinct disposables with counts (used for > MaximumLinearSearchThreshold items), it null-checks each element during enumeration. Any null element makes the composite unusable on Dispose, so ArgumentException with parameter 'disposables' is thrown immediately.

Solutions

  1. Filter nulls before constructing: items.Where(d => d != null)
  2. Throw or log on nulls upstream instead of silently passing them, so missing disposables are detected at the source
  3. Use OfType<IDisposable>() when the source is loosely typed to drop nulls

Example fix

// before
var cd = new CompositeDisposable(rawItems); // may contain nulls
// after
var cd = new CompositeDisposable(rawItems.Where(d => d != null));
Defensive patterns

Strategy: validation

Validate before calling

if (items.Any(d => d == null)) throw new InvalidOperationException("Collection contains null disposables");
var cd = new CompositeDisposable(items);

Type guard

bool AllNonNull(IEnumerable<IDisposable?> items) => items.All(d => d is not null);

Try / catch

try { return new CompositeDisposable(items); }
catch (ArgumentException ex) { /* report which collection had nulls */ throw; }

Prevention

When it happens

Trigger: Constructing CompositeDisposable from a large collection (> MaximumLinearSearchThreshold, i.e. the dictionary path) that contains at least one null IDisposable element.

Common situations: Aggregating disposables from DI containers or event subscriptions where some entries are null placeholders; LINQ Select producing nulls; passing an object[] cast to IDisposable[] with null holes.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

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

        private static (object Collection, int Count) ToListOrDictionary(IEnumerable<IDisposable> disposables)
        {
            var capacity = disposables switch
            {
                IDisposable[] a => a.Length,
                ICollection<IDisposable> c => c.Count,
                _ => DefaultCapacity
            };

            if (capacity > MaximumLinearSearchThreshold)
            {
                var dictionary = new Dictionary<IDisposable, int>(capacity);
                var disposableCount = 0;
                foreach (var d in disposables)
                {
                    if (d == null)
                    {
                        throw new ArgumentException(Strings_Core.DISPOSABLES_CANT_CONTAIN_NULL, nameof(disposables));
                    }

                    dictionary.TryGetValue(d, out var thisDisposableCount);
                    dictionary[d] = thisDisposableCount + 1;

                    disposableCount += 1;
                }

                return (dictionary, disposableCount);
            }

            var list = new List<IDisposable?>(capacity);

            // do the copy and null-check in one step to avoid a
            // second loop for just checking for null items
            foreach (var d in disposables)
            {
                if (d == null)

View on GitHub (pinned to 94b5d5ab91)