{"record":{"id":"cdba2f75e26829d9","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-array","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'array')","messagePattern":"Value cannot be null\\. \\(Parameter 'array'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Disposables/CompositeDisposable.cs","lineNumber":427,"sourceCode":"                var current = _disposables;\n                return current is List<IDisposable?> list\n                    ? list.Contains(item)\n                    : ((Dictionary<IDisposable, int>) current).ContainsKey(item);\n            }\n        }\n\n        /// <summary>\n        /// Copies the disposables contained in the <see cref=\"CompositeDisposable\"/> to an array, starting at a particular array index.\n        /// </summary>\n        /// <param name=\"array\">Array to copy the contained disposables to.</param>\n        /// <param name=\"arrayIndex\">Target index at which to copy the first disposable of the group.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"array\"/> is <c>null</c>.</exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"arrayIndex\"/> is less than zero. -or - <paramref name=\"arrayIndex\"/> is larger than or equal to the array length.</exception>\n        public void CopyTo(IDisposable[] array, int arrayIndex)\n        {\n            if (array == null)\n            {\n                throw new ArgumentNullException(nameof(array));\n            }\n\n            if (arrayIndex < 0 || arrayIndex >= array.Length)\n            {\n                throw new ArgumentOutOfRangeException(nameof(arrayIndex));\n            }\n\n            lock (_gate)\n            {\n                // disposed composites are always empty\n                if (_disposed)\n                {\n                    return;\n                }\n\n                if (arrayIndex + _count > array.Length)\n                {\n                    // there is not enough space beyond arrayIndex ","sourceCodeStart":409,"sourceCodeEnd":445,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Disposables/CompositeDisposable.cs#L409-L445","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Allocate the array before copying: var snapshot = new IDisposable[composite.Count]; composite.CopyTo(snapshot, 0);","Prefer composite.ToArray() (LINQ) which handles allocation internally.","Null-check the destination array before calling CopyTo."],"exampleFix":"// before\nIDisposable[] snapshot = GetBuffer(); // may be null\ncomposite.CopyTo(snapshot, 0);\n// after\nvar snapshot = new IDisposable[composite.Count];\ncomposite.CopyTo(snapshot, 0);","handlingStrategy":"validation","validationCode":"var arr = destination ?? new IDisposable[composite.Count];\ncomposite.CopyTo(arr, 0);","typeGuard":"bool CanCopy(CompositeDisposable c, IDisposable[] a) => c != null && a != null;","tryCatchPattern":"try { composite.CopyTo(array, index); } catch (ArgumentNullException ex) when (ex.ParamName == \"array\") { array = new IDisposable[composite.Count]; composite.CopyTo(array, index); }","preventionTips":["Prefer composite.ToArray() over manual CopyTo","Always allocate the destination array immediately before copying","Never accept arrays from callers that may return null"],"tags":["argument-null","copyto","csharp","rx"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}