TheAlgorithms/C-Sharp · error · ArgumentOutOfRangeException
Capacity must be greater than 0
Error message
Capacity must be greater than 0
What it means
The HashTable constructor validates its parameters: capacity <= 0 throws ArgumentOutOfRangeException(nameof(capacity), "Capacity must be greater than 0"), and loadFactor <= 0 is rejected similarly. Capacity must be a positive integer because the table allocates bucket storage of at least that size (typically the next prime).
Solutions
- Pass a positive capacity, e.g. new HashTable<K,V>(100).
- Omit the parameter to use DefaultCapacity instead of passing 0.
- Clamp before constructing: capacity = Math.Max(1, requestedCapacity).
Example fix
// before var capacity = config.ExpectedEntries ?? 0; // 0 -> throws var table = new HashTable<string, int>(capacity); // after var capacity = Math.Max(1, config.ExpectedEntries ?? DefaultCapacity); var table = new HashTable<string, int>(capacity);
Defensive patterns
Strategy: validation
Validate before calling
static bool IsValidCapacity(int capacity) => capacity > 0; // call: if (!IsValidCapacity(cap)) cap = DefaultCapacity;
Type guard
static bool IsPositive(int v) => v > 0;
Try / catch
try { var t = new HashTable<K,V>(cap, lf); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "capacity") { var t = new HashTable<K,V>(); } Prevention
- Treat 0 as invalid; omit the argument to use DefaultCapacity.
- Clamp config-derived capacities with Math.Max(1, value).
- Also validate loadFactor > 0 when passing it explicitly.
When it happens
Trigger: new HashTable<K,V>(capacity: 0), new HashTable<K,V>(-5), or a capacity computed from input/config that is zero or negative.
Common situations: Config files with unset numeric fields defaulting to 0, computing capacity as expectedCount - removed where removed >= expectedCount, or passing 0 intending 'use default'.
Related errors
- k must be at least 1.
- Invalid block size
- Not enough space in input array for padding
- Vertex indices must be within valid range.
- Double factorial is only defined for non-negative integers…
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/65dc6e40a0360010.
Report an issue: GitHub.
Appendix: source
Thrown at DataStructures/Hashing/HashTable.cs:95
/// <summary>
/// Initializes a new instance of the <see cref="HashTable{TKey, TValue}"/> class.
/// </summary>
/// <param name="capacity">Initial capacity of the hash table.</param>
/// <param name="loadFactor">Load factor of the hash table.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="capacity"/> is less than or equal to 0.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="loadFactor"/> is less than or equal to 0.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="loadFactor"/> is greater than 1.</exception>
/// <remarks>
/// <paramref name="capacity"/> is rounded to the next prime number.
/// </remarks>
/// <see cref="PrimeNumber.NextPrime(int, int, bool)"/>
/// <see cref="PrimeNumber.IsPrime(int)"/>
public HashTable(int capacity = DefaultCapacity, float loadFactor = DefaultLoadFactor)
{
if (capacity <= 0)
{
throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be greater than 0");
}
if (loadFactor <= 0)
{
throw new ArgumentOutOfRangeException(nameof(loadFactor), "Load factor must be greater than 0");
}
if (loadFactor > 1)
{
throw new ArgumentOutOfRangeException(nameof(loadFactor), "Load factor must be less than or equal to 1");
}
this.capacity = PrimeNumber.NextPrime(capacity);
this.loadFactor = loadFactor;
threshold = (int)(this.capacity * loadFactor);
entries = new Entry<TKey, TValue>[this.capacity];
}
View on GitHub (pinned to 96e2905cab)