jstedfast/MailKit · error · ArgumentOutOfRangeException

Specified argument was out of range of valid values…

Error message

Specified argument was out of range of valid values. (Parameter 'index')

What it means

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.

Solutions

  1. Loop with i < accessRights.Count, or iterate with foreach instead of indexing.
  2. Check index against Count before accessing: if (i >= 0 && i < accessRights.Count).
  3. Re-read Count immediately before indexing if the collection may have changed.

Example fix

// before
for (int i = 0; i <= accessRights.Count; i++)
    Console.WriteLine(accessRights[i]);

// after
for (int i = 0; i < accessRights.Count; i++)
    Console.WriteLine(accessRights[i]);
Defensive patterns

Strategy: validation

Validate before calling

if (index >= 0 && index < accessRights.Count)
{
    var right = accessRights[index];
}

Type guard

bool InRange(AccessRights rights, int i) => i >= 0 && i < rights.Count;

Try / catch

try
{
    var right = accessRights[index];
}
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "index")
{
    logger.LogError("Index {0} out of range for {1} rights", index, accessRights.Count);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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

Appendix: source

Thrown at MailKit/AccessRights.cs:262

		{
			return list.Remove (right);
		}

		/// <summary>
		/// Get the access right at the specified index.
		/// </summary>
		/// <remarks>
		/// Gets the access right at the specified index.
		/// </remarks>
		/// <value>The access right at the specified index.</value>
		/// <param name="index">The index.</param>
		/// <exception cref="System.ArgumentOutOfRangeException">
		/// <paramref name="index"/> is out of range.
		/// </exception>
		public AccessRight this [int index] {
			get {
				if (index < 0 || index >= list.Count)
					throw new ArgumentOutOfRangeException (nameof (index));

				return list[index];
			}
		}

		#region IEnumerable implementation

		/// <summary>
		/// Get the access rights enumerator.
		/// </summary>
		/// <remarks>
		/// Gets the access rights enumerator.
		/// </remarks>
		/// <returns>The enumerator.</returns>
		public IEnumerator<AccessRight> GetEnumerator ()
		{
			return list.GetEnumerator ();
		}

View on GitHub (pinned to 9d3859a785)