{"record":{"id":"653d85b7c61a6d69","repo":"jstedfast/MailKit","slug":"specified-argument-was-out-of-range-of-valid-values-653d85","errorCode":null,"errorMessage":"Specified argument was out of range of valid values. (Parameter 'index')","messagePattern":"Specified argument was out of range of valid values\\. \\(Parameter 'index'\\)","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"MailKit/AccessRights.cs","lineNumber":262,"sourceCode":"\t\t{\n\t\t\treturn list.Remove (right);\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Get the access right at the specified index.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Gets the access right at the specified index.\n\t\t/// </remarks>\n\t\t/// <value>The access right 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 AccessRight this [int index] {\n\t\t\tget {\n\t\t\t\tif (index < 0 || index >= list.Count)\n\t\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (index));\n\n\t\t\t\treturn list[index];\n\t\t\t}\n\t\t}\n\n\t\t#region IEnumerable implementation\n\n\t\t/// <summary>\n\t\t/// Get the access rights enumerator.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Gets the access rights enumerator.\n\t\t/// </remarks>\n\t\t/// <returns>The enumerator.</returns>\n\t\tpublic IEnumerator<AccessRight> GetEnumerator ()\n\t\t{\n\t\t\treturn list.GetEnumerator ();\n\t\t}","sourceCodeStart":244,"sourceCodeEnd":280,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/AccessRights.cs#L244-L280","documentation":"The AccessRights this[int index] indexer reads a single AccessRight by position and throws ArgumentOutOfRangeException(nameof(index)) at MailKit/AccessRights.cs:262 when index is negative or >= list.Count. Valid indices run from 0 to Count - 1.","triggerScenarios":"Reading accessRights[i] where i is negative or i >= accessRights.Count — e.g. iterating to Count inclusive, indexing after the collection was modified, or using an index from another collection.","commonSituations":"Looping with <= instead of <; caching a count from before items were removed; assuming a minimum count that the server's GETACL response did not provide.","solutions":["Loop with i < accessRights.Count, or iterate with foreach instead of indexing.","Check index against Count before accessing: if (i >= 0 && i < accessRights.Count).","Re-read Count immediately before indexing if the collection may have changed."],"exampleFix":"// before\nfor (int i = 0; i <= accessRights.Count; i++)\n    Console.WriteLine(accessRights[i]);\n\n// after\nfor (int i = 0; i < accessRights.Count; i++)\n    Console.WriteLine(accessRights[i]);","handlingStrategy":"validation","validationCode":"if (index >= 0 && index < accessRights.Count)\n{\n    var right = accessRights[index];\n}","typeGuard":"bool InRange(AccessRights rights, int i) => i >= 0 && i < rights.Count;","tryCatchPattern":"try\n{\n    var right = accessRights[index];\n}\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"index\")\n{\n    logger.LogError(\"Index {0} out of range for {1} rights\", index, accessRights.Count);\n}","preventionTips":["Use foreach instead of manual indexing whenever possible","Loop with i < Count, never <= Count","Recheck Count after any Add/Remove before indexing"],"tags":["argument-out-of-range","indexer","imap","mailkit"],"backgroundTag":"argument-out-of-range","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"}