TheAlgorithms/C-Sharp · error · ArgumentOutOfRangeException
Load factor must be greater than 0
Error message
Load factor must be greater than 0
What it means
The HashTable constructor throws ArgumentOutOfRangeException when the loadFactor parameter is zero or negative. Load factor controls how full the table may get before resizing; a non-positive value makes the resize threshold meaningless (zero or negative), so the library rejects it eagerly at construction time.
Solutions
- Pass a load factor greater than 0 and <= 1 (a common choice is 0.75).
- If the value comes from config, validate with double.TryParse and fall back to 0.75 when missing or out of range.
- Guard derived values: Math.Max(0.01, Math.Min(1.0, computedLoadFactor)) before constructing.
Example fix
// before var ht = new HashTable<string, int>(16, config.LoadFactor); // LoadFactor may be 0 // after var lf = config.LoadFactor > 0 && config.LoadFactor <= 1 ? config.LoadFactor : 0.75; var ht = new HashTable<string, int>(16, lf);
Defensive patterns
Strategy: validation
Validate before calling
if (double.IsNaN(loadFactor) || loadFactor <= 0 || loadFactor > 1)
throw new ArgumentOutOfRangeException(nameof(loadFactor), "Load factor must be in (0, 1]"); Type guard
static bool IsValidLoadFactor(double lf) => !double.IsNaN(lf) && lf > 0 && lf <= 1;
Try / catch
try { var ht = new HashTable<string,int>(cap, loadFactor); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "loadFactor")
{
loadFactor = 0.75; // fallback default
} Prevention
- Centralize table construction in a factory that clamps load factor to (0, 1].
- Never feed parsed config straight into the constructor without range validation.
- Use 0.75 as the standard default.
When it happens
Trigger: Calling new HashTable<TKey,TValue>(capacity, loadFactor) with loadFactor <= 0, e.g. new HashTable<string,int>(10, 0) or new HashTable<string,int>(10, -0.5f), or passing a computed/derived load factor expression that evaluates to zero or negative.
Common situations: Configuration values read from app settings or environment variables where the load factor is parsed into a double that defaults to 0 when the setting is missing or fails to parse; math that computes a ratio like used/total yielding 0 on an empty table and being fed straight into the constructor.
Related errors
- Load factor must be less than or equal to 1
- k must be at least 1.
- Number of colors must be positive.
- Board size must be positive.
- Capacity must be greater than 0
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/8b32815a15d78946.
Report an issue: GitHub.
Appendix: source
Thrown at DataStructures/Hashing/HashTable.cs:100
/// <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];
}
/// <summary>
/// Adds a key-value pair to the hash table.
/// </summary>
/// <param name="key">Key to add.</param>
/// <param name="value">Value to add.</param>View on GitHub (pinned to 96e2905cab)