{"record":{"id":"d2898d84cce36aba","repo":"jstedfast/MailKit","slug":"index","errorCode":null,"errorMessage":"index","messagePattern":"index","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"MailKit/BodyPartCollection.cs","lineNumber":184,"sourceCode":"\n\t\t\treturn collection.Remove (part);\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Get the body part at the specified index.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Gets the body part at the specified index.\n\t\t/// </remarks>\n\t\t/// <value>The body part at the specified index.</value>\n\t\t/// <param name=\"index\">The index.</param>\n\t\t/// <exception cref=\"System.ArgumentOutOfRangeException\">\n\t\t/// <paramref name=\"index\"/> is out of range.\n\t\t/// </exception>\n\t\tpublic BodyPart this [int index] {\n\t\t\tget {\n\t\t\t\tif (index < 0 || index >= collection.Count)\n\t\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (index));\n\n\t\t\t\treturn collection[index];\n\t\t\t}\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Gets the index of the body part matching the specified URI.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// <para>Finds the index of the body part matching the specified URI, if it exists.</para>\n\t\t/// <para>If the URI scheme is <c>\"cid\"</c>, then matching is performed based on the Content-Id header\n\t\t/// values, otherwise the Content-Location headers are used. If the provided URI is absolute and a child\n\t\t/// part's Content-Location is relative, then then the child part's Content-Location URI will be combined\n\t\t/// with the value of its Content-Base header, if available, otherwise it will be combined with the\n\t\t/// multipart/related part's Content-Base header in order to produce an absolute URI that can be\n\t\t/// compared with the provided absolute URI.</para>\n\t\t/// </remarks>\n\t\t/// <returns>The index of the part matching the specified URI if found; otherwise <c>-1</c>.</returns>","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/BodyPartCollection.cs#L166-L202","documentation":"The BodyPartCollection this[int index] indexer throws ArgumentOutOfRangeException when index is negative or >= collection.Count. MailKit's indexer performs explicit bounds checks and reports the offending parameter as 'index'.","triggerScenarios":"Accessing bodyParts[i] where i < 0 or i >= bodyParts.Count, e.g. looping with <= instead of <, or assuming a non-empty collection without checking Count.","commonSituations":"Loop bounds off-by-one when walking multipart children; assuming a message has at least one body part; stale code after server responses changed so a part is missing; counting nested parts with the wrong Count source.","solutions":["Check Count before indexing: for (int i = 0; i < bodyParts.Count; i++) ...","Verify the message actually has body parts (guard against empty collections) before [0].","Enumerate with foreach (BodyPart part in bodyParts) to eliminate index arithmetic."],"exampleFix":"// before\nfor (int i = 0; i <= bodyParts.Count; i++)\n    Process(bodyParts[i]);\n// after\nfor (int i = 0; i < bodyParts.Count; i++)\n    Process(bodyParts[i]);","handlingStrategy":"validation","validationCode":"bool inRange = index >= 0 && index < bodyParts.Count;","typeGuard":"bool TryGetPart(BodyPartCollection parts, int index, out BodyPart value) { value = null; if (index < 0 || index >= parts.Count) return false; value = parts[index]; return true; }","tryCatchPattern":"try { part = bodyParts[index]; } catch (ArgumentOutOfRangeException) { part = null; }","preventionTips":["Always loop with i < collection.Count, never <=","Check Count > 0 before accessing [0]","Prefer foreach enumeration over index access","Re-verify assumptions about message structure when server behavior changes"],"tags":["argument-out-of-range","indexer","bounds-check","mailkit"],"backgroundTag":"index-out-of-bounds","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"}