jstedfast/MailKit · error · ArgumentOutOfRangeException

arrayIndex

Error message

arrayIndex

What it means

BodyPartCollection.CopyTo throws ArgumentOutOfRangeException when arrayIndex is negative or arrayIndex + Count exceeds array.Length, i.e. there is not enough room in the destination array at the given offset. The parameter name 'arrayIndex' is passed as the exception's paramName.

Solutions

  1. Size the destination as bodyParts.Count + arrayIndex or start at index 0: bodyParts.CopyTo(array, 0).
  2. Validate before calling: if (arrayIndex >= 0 && arrayIndex + bodyParts.Count <= array.Length).
  3. Use foreach/ToArray() to avoid manual index math entirely.

Example fix

// before
var array = new BodyPart[collection.Count];
collection.CopyTo(array, 1); // out of range
// after
var array = new BodyPart[collection.Count];
collection.CopyTo(array, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (arrayIndex < 0 || arrayIndex + bodyParts.Count > array.Length) throw new ArgumentException("Insufficient space in destination array", nameof(arrayIndex));

Type guard

bool FitsInArray(int count, int index, int length) => index >= 0 && index + count <= length;

Try / catch

try { bodyParts.CopyTo(array, offset); } catch (ArgumentOutOfRangeException ex) { /* resize array or reset offset */ }

Prevention

When it happens

Trigger: Calling CopyTo(array, -1); calling CopyTo(array, 1) on a 1-element collection into a 1-element array; copying Count items at an offset that leaves fewer than Count slots free.

Common situations: Manually sized arrays that don't account for Count; off-by-one offsets when writing multiple collections into one shared buffer; using array.Length of a differently-sized array than intended after refactoring.

Related errors


AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15). Data as JSON: /api/errors/2d567caed10ee9d9. Report an issue: GitHub.

Appendix: source

Thrown at MailKit/BodyPartCollection.cs:146

		/// <remarks>
		/// Copies all of the body parts within the collection into the array,
		/// starting at the specified array index.
		/// </remarks>
		/// <param name="array">The array.</param>
		/// <param name="arrayIndex">The array index.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="array"/> is <see langword="null" />.
		/// </exception>
		/// <exception cref="System.ArgumentOutOfRangeException">
		/// <paramref name="arrayIndex"/> is out of range.
		/// </exception>
		public void CopyTo (BodyPart[] array, int arrayIndex)
		{
			if (array == null)
				throw new ArgumentNullException (nameof (array));

			if (arrayIndex < 0 || arrayIndex + Count > array.Length)
				throw new ArgumentOutOfRangeException (nameof (arrayIndex));

			collection.CopyTo (array, arrayIndex);
		}

		/// <summary>
		/// Removes the specified body part.
		/// </summary>
		/// <remarks>
		/// Removes the specified body part.
		/// </remarks>
		/// <returns><see langword="true" /> if the body part was removed; otherwise, <see langword="false" />.</returns>
		/// <param name="part">The body part.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="part"/> is <see langword="null" />.
		/// </exception>
		public bool Remove (BodyPart part)
		{
			if (part == null)

View on GitHub (pinned to 9d3859a785)