{"record":{"id":"72692b3d7ea94232","repo":"AvaloniaUI/Avalonia","slug":"array-72692b","errorCode":null,"errorMessage":"array","messagePattern":"array","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Collections/Pooled/PooledList.cs","lineNumber":661,"sourceCode":"        /// </summary>\n        public void CopyTo(Span<T> span)\n        {\n            if (span.Length < Count)\n                throw new ArgumentException(\"Destination span is shorter than the list to be copied.\");\n\n            Span.CopyTo(span);\n        }\n\n        void ICollection<T>.CopyTo(T[] array, int arrayIndex)\n        {\n            Array.Copy(_items, 0, array, arrayIndex, _size);\n        }\n\n        // Copies this List into array, which must be of a \n        // compatible array type.  \n        void ICollection.CopyTo(Array array, int arrayIndex)\n        {\n            _ = array ?? throw new ArgumentNullException(nameof(array));\n\n            if (array.Rank != 1)\n            {\n                ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);\n            }\n\n            try\n            {\n                Array.Copy(_items, 0, array, arrayIndex, _size);\n            }\n            catch (ArrayTypeMismatchException)\n            {\n                ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType();\n            }\n        }\n\n        /// <summary>\n        /// Ensures that the capacity of this list is at least the given minimum","sourceCodeStart":643,"sourceCodeEnd":679,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Collections/Pooled/PooledList.cs#L643-L679","documentation":"ArgumentNullException(\"array\") from PooledList<T>.ICollection.CopyTo(Array, int) when the target array is null. The guard `_ = array ?? throw` runs before the rank check and Array.Copy, so a null destination array fails fast. It mirrors the standard ICollection.CopyTo contract.","triggerScenarios":"Invoking the non-generic CopyTo with a null array: `((ICollection)list).CopyTo(null!, 0)`, or passing an uninitialized array variable.","commonSituations":"Passing a null array from an uninitialized local; LINQ/collection-init paths that hand a null Array through; generic code forwarding a possibly-null array parameter.","solutions":["Allocate the destination array before calling: `var arr = new T[list.Count]; ((ICollection)list).CopyTo(arr, 0);`.","Prefer the strongly-typed CopyTo(T[], int) overload or CopyTo(Span<T>) which make intent explicit.","Null-check the array parameter in your own code before forwarding it."],"exampleFix":"// before\nT[] arr = null;\n((ICollection)list).CopyTo(arr, 0);\n\n// after\nT[] arr = new T[list.Count];\n((ICollection)list).CopyTo(arr, 0);","handlingStrategy":"validation","validationCode":"if (array is null) throw new ArgumentNullException(nameof(array));\n((ICollection)list).CopyTo(array, index);","typeGuard":"static bool IsUsable(Array a) => a is not null && a.Rank == 1;","tryCatchPattern":null,"preventionTips":["Allocate the destination array before calling CopyTo.","Prefer the generic CopyTo(T[], int) over the non-generic ICollection.CopyTo.","Null-check forwarded array parameters in your own API."],"tags":["csharp","pooled","collections","null-argument","copy"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}