{"record":{"id":"2963001793ba7d90","repo":"jstedfast/MailKit","slug":"array","errorCode":null,"errorMessage":"array","messagePattern":"array","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"MailKit/BodyPartCollection.cs","lineNumber":143,"sourceCode":"\t\t/// <summary>\n\t\t/// Copies all of the body parts in the collection to the specified array.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Copies all of the body parts within the collection into the array,\n\t\t/// starting at the specified array index.\n\t\t/// </remarks>\n\t\t/// <param name=\"array\">The array.</param>\n\t\t/// <param name=\"arrayIndex\">The array index.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <paramref name=\"array\"/> is <see langword=\"null\" />.\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.ArgumentOutOfRangeException\">\n\t\t/// <paramref name=\"arrayIndex\"/> is out of range.\n\t\t/// </exception>\n\t\tpublic void CopyTo (BodyPart[] array, int arrayIndex)\n\t\t{\n\t\t\tif (array == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (array));\n\n\t\t\tif (arrayIndex < 0 || arrayIndex + Count > array.Length)\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (arrayIndex));\n\n\t\t\tcollection.CopyTo (array, arrayIndex);\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Removes the specified body part.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Removes the specified body part.\n\t\t/// </remarks>\n\t\t/// <returns><see langword=\"true\" /> if the body part was removed; otherwise, <see langword=\"false\" />.</returns>\n\t\t/// <param name=\"part\">The body part.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <paramref name=\"part\"/> is <see langword=\"null\" />.\n\t\t/// </exception>","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/BodyPartCollection.cs#L125-L161","documentation":"BodyPartCollection.CopyTo throws ArgumentNullException when the destination BodyPart[] array is null. The collection cannot copy elements into a nonexistent array, so the null array is rejected immediately with the parameter name 'array'.","triggerScenarios":"Calling CopyTo(null, 0) or passing an uninitialized BodyPart[] variable (e.g. a field never assigned) into BodyPartCollection.CopyTo(array, arrayIndex).","commonSituations":"Test suites (TestBodyPartCollection) exercising null-array behavior; refactoring code where the array allocation was removed or moved after the CopyTo call; copying body parts into a buffer sized later but passed before assignment.","solutions":["Allocate the destination array before calling CopyTo: var array = new BodyPart[bodyParts.Count];","Guard with if (array != null) bodyParts.CopyTo(array, 0) when the array comes from an optional source.","Use ToArray()/LINQ or foreach enumeration instead of manual CopyTo when you just need a snapshot."],"exampleFix":"// before\nBodyPart[] array = null;\nbodyParts.CopyTo(array, 0);\n// after\nBodyPart[] array = new BodyPart[bodyParts.Count];\nbodyParts.CopyTo(array, 0);","handlingStrategy":"validation","validationCode":"if (array == null) throw new ArgumentException(\"Destination array must not be null\", nameof(array));","typeGuard":"bool CanCopyTo(BodyPartCollection src, BodyPart[] array, int idx) => array != null && idx >= 0 && idx + src.Count <= array.Length;","tryCatchPattern":"try { bodyParts.CopyTo(array, 0); } catch (ArgumentNullException ex) { /* handle missing destination */ }","preventionTips":["Allocate the array with collection.Count elements before copying","Prefer ToArray()/foreach over manual CopyTo","Keep array allocation and CopyTo adjacent so refactors don't separate them"],"tags":["null-reference","argument-validation","copyto","mailkit"],"backgroundTag":"null-argument","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}