{"record":{"id":"405fa1e1cf42954f","repo":"dotnet/reactive","slug":"disposables-collection-can-not-contain-null-values-parameter","errorCode":null,"errorMessage":"Disposables collection can not contain null values. (Parameter 'disposables')","messagePattern":"Disposables collection can not contain null values\\. \\(Parameter 'disposables'\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Disposables/CompositeDisposable.cs","lineNumber":115,"sourceCode":"\n        private static (object Collection, int Count) ToListOrDictionary(IEnumerable<IDisposable> disposables)\n        {\n            var capacity = disposables switch\n            {\n                IDisposable[] a => a.Length,\n                ICollection<IDisposable> c => c.Count,\n                _ => DefaultCapacity\n            };\n\n            if (capacity > MaximumLinearSearchThreshold)\n            {\n                var dictionary = new Dictionary<IDisposable, int>(capacity);\n                var disposableCount = 0;\n                foreach (var d in disposables)\n                {\n                    if (d == null)\n                    {\n                        throw new ArgumentException(Strings_Core.DISPOSABLES_CANT_CONTAIN_NULL, nameof(disposables));\n                    }\n\n                    dictionary.TryGetValue(d, out var thisDisposableCount);\n                    dictionary[d] = thisDisposableCount + 1;\n\n                    disposableCount += 1;\n                }\n\n                return (dictionary, disposableCount);\n            }\n\n            var list = new List<IDisposable?>(capacity);\n\n            // do the copy and null-check in one step to avoid a\n            // second loop for just checking for null items\n            foreach (var d in disposables)\n            {\n                if (d == null)","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Disposables/CompositeDisposable.cs#L97-L133","documentation":"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.","triggerScenarios":"Constructing CompositeDisposable from a large collection (> MaximumLinearSearchThreshold, i.e. the dictionary path) that contains at least one null IDisposable element.","commonSituations":"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.","solutions":["Filter nulls before constructing: items.Where(d => d != null)","Throw or log on nulls upstream instead of silently passing them, so missing disposables are detected at the source","Use OfType<IDisposable>() when the source is loosely typed to drop nulls"],"exampleFix":"// before\nvar cd = new CompositeDisposable(rawItems); // may contain nulls\n// after\nvar cd = new CompositeDisposable(rawItems.Where(d => d != null));","handlingStrategy":"validation","validationCode":"if (items.Any(d => d == null)) throw new InvalidOperationException(\"Collection contains null disposables\");\nvar cd = new CompositeDisposable(items);","typeGuard":"bool AllNonNull(IEnumerable<IDisposable?> items) => items.All(d => d is not null);","tryCatchPattern":"try { return new CompositeDisposable(items); }\ncatch (ArgumentException ex) { /* report which collection had nulls */ throw; }","preventionTips":["Filter with Where(d => d != null) before composing","Replace null slots with Disposable.Empty","Enforce non-null disposable production at the source (contracts/tests)"],"tags":["rx","disposable","null-item","argument"],"backgroundTag":"invalid-argument-value","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}