jstedfast/MailKit · error · ArgumentOutOfRangeException
ArgumentOutOfRangeException
Error message
ArgumentOutOfRangeException
What it means
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.
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
Example fix
// before var ns = namespaces[0]; // after var ns = namespaces.Count > 0 ? namespaces[0] : null;
Defensive patterns
Strategy: validation
Validate before calling
FolderNamespace GetNs (FolderNamespaceCollection c, int i) =>
(i >= 0 && i < c.Count) ? c[i] : null; Type guard
bool InRange (int i, int count) => i >= 0 && i < count;
Try / catch
try {
ns = namespaces[index];
} catch (ArgumentOutOfRangeException) {
ns = null; // collection empty or index invalid
} Prevention
- Check Count before positional access, especially index 0
- Use foreach instead of index loops
- Remember NAMESPACE responses may legitimately contain zero personal namespaces
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- ArgumentNullException
- One or more of the messages is null.
- The number of messages and the number of flags must be…
- The uid is invalid.
- part
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/a54f42129b0be860.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/FolderNamespaceCollection.cs:155
/// <summary>
/// Gets the <see cref="MailKit.FolderNamespace"/> at the specified index.
/// </summary>
/// <remarks>
/// Gets the <see cref="MailKit.FolderNamespace"/> at the specified index.
/// </remarks>
/// <value>The folder namespace at the specified index.</value>
/// <param name="index">The index.</param>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="value"/> is <see langword="null" />.
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// <paramref name="index"/> is out of range.
/// </exception>
public FolderNamespace this [int index] {
get {
if (index < 0 || index >= namespaces.Count)
throw new ArgumentOutOfRangeException (nameof (index));
return namespaces[index];
}
set {
if (index < 0 || index >= namespaces.Count)
throw new ArgumentOutOfRangeException (nameof (index));
if (value == null)
throw new ArgumentNullException (nameof (value));
namespaces[index] = value;
}
}
#endregion
#region IEnumerable implementation
View on GitHub (pinned to 9d3859a785)