{"record":{"id":"a54f42129b0be860","repo":"jstedfast/MailKit","slug":"argumentoutofrangeexception-foldernamespacecollection","errorCode":null,"errorMessage":"ArgumentOutOfRangeException","messagePattern":"ArgumentOutOfRangeException","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"MailKit/FolderNamespaceCollection.cs","lineNumber":155,"sourceCode":"\n\t\t/// <summary>\n\t\t/// Gets the <see cref=\"MailKit.FolderNamespace\"/> at the specified index.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Gets the <see cref=\"MailKit.FolderNamespace\"/> at the specified index.\n\t\t/// </remarks>\n\t\t/// <value>The folder namespace at the specified index.</value>\n\t\t/// <param name=\"index\">The index.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <paramref name=\"value\"/> is <see langword=\"null\" />.\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.ArgumentOutOfRangeException\">\n\t\t/// <paramref name=\"index\"/> is out of range.\n\t\t/// </exception>\n\t\tpublic FolderNamespace this [int index] {\n\t\t\tget {\n\t\t\t\tif (index < 0 || index >= namespaces.Count)\n\t\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (index));\n\n\t\t\t\treturn namespaces[index];\n\t\t\t}\n\t\t\tset {\n\t\t\t\tif (index < 0 || index >= namespaces.Count)\n\t\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (index));\n\n\t\t\t\tif (value == null)\n\t\t\t\t\tthrow new ArgumentNullException (nameof (value));\n\n\t\t\t\tnamespaces[index] = value;\n\t\t\t}\n\t\t}\n\n\t\t#endregion\n\n\t\t#region IEnumerable implementation\n","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/FolderNamespaceCollection.cs#L137-L173","documentation":"The FolderNamespaceCollection indexer getter (this[int index]) throws ArgumentOutOfRangeException when index is negative or >= namespaces.Count. This is the documented out-of-range guard for reading a namespace by position.","triggerScenarios":"Reading collection[index] with index < 0 or index >= collection.Count, e.g. hard-coded index 0 on an empty collection returned by ImapClient.GetFolders or the NAMESPACE response.","commonSituations":"Assuming the server always returns at least one personal namespace; looping with an off-by-one bound (<= Count); copying code that assumed a non-empty namespace list.","solutions":["Check index against collection.Count before indexing","Check Count > 0 before accessing element 0","Use foreach enumeration instead of positional access when processing all namespaces"],"exampleFix":"// before\nvar ns = namespaces[0];\n// after\nvar ns = namespaces.Count > 0 ? namespaces[0] : null;","handlingStrategy":"validation","validationCode":"FolderNamespace GetNs (FolderNamespaceCollection c, int i) =>\n    (i >= 0 && i < c.Count) ? c[i] : null;","typeGuard":"bool InRange (int i, int count) => i >= 0 && i < count;","tryCatchPattern":"try {\n    ns = namespaces[index];\n} catch (ArgumentOutOfRangeException) {\n    ns = null; // collection empty or index invalid\n}","preventionTips":["Check Count before positional access, especially index 0","Use foreach instead of index loops","Remember NAMESPACE responses may legitimately contain zero personal namespaces"],"tags":["csharp","mailkit","index-out-of-range","argument-validation"],"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"}