{"record":{"id":"6c0df8a1b4bbbc36","repo":"TheAlgorithms/C-Sharp","slug":"load-factor-must-be-less-than-or-equal-to-1","errorCode":null,"errorMessage":"Load factor must be less than or equal to 1","messagePattern":"Load factor must be less than or equal to 1","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"DataStructures/Hashing/HashTable.cs","lineNumber":105,"sourceCode":"    /// <paramref name=\"capacity\"/> is rounded to the next prime number.\n    /// </remarks>\n    /// <see cref=\"PrimeNumber.NextPrime(int, int, bool)\"/>\n    /// <see cref=\"PrimeNumber.IsPrime(int)\"/>\n    public HashTable(int capacity = DefaultCapacity, float loadFactor = DefaultLoadFactor)\n    {\n        if (capacity <= 0)\n        {\n            throw new ArgumentOutOfRangeException(nameof(capacity), \"Capacity must be greater than 0\");\n        }\n\n        if (loadFactor <= 0)\n        {\n            throw new ArgumentOutOfRangeException(nameof(loadFactor), \"Load factor must be greater than 0\");\n        }\n\n        if (loadFactor > 1)\n        {\n            throw new ArgumentOutOfRangeException(nameof(loadFactor), \"Load factor must be less than or equal to 1\");\n        }\n\n        this.capacity = PrimeNumber.NextPrime(capacity);\n        this.loadFactor = loadFactor;\n        threshold = (int)(this.capacity * loadFactor);\n        entries = new Entry<TKey, TValue>[this.capacity];\n    }\n\n    /// <summary>\n    /// Adds a key-value pair to the hash table.\n    /// </summary>\n    /// <param name=\"key\">Key to add.</param>\n    /// <param name=\"value\">Value to add.</param>\n    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"key\"/> is null.</exception>\n    /// <exception cref=\"ArgumentException\">Thrown when <paramref name=\"key\"/> already exists in the hash table.</exception>\n    /// <remarks>\n    /// If the number of elements in the hash table is greater than or equal to the threshold, the hash table is resized.\n    /// </remarks>","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Hashing/HashTable.cs#L87-L123","documentation":"The HashTable constructor throws ArgumentOutOfRangeException when loadFactor exceeds 1. This implementation resizes only when count >= threshold, so a load factor above 1 would allow the table to exceed capacity before resizing, degrading (or breaking) the invariant that entries fit in the backing array.","triggerScenarios":"Calling new HashTable<TKey,TValue>(capacity, loadFactor) with loadFactor > 1, e.g. new HashTable<string,int>(10, 1.5) or a percentage supplied as 75 instead of 0.75.","commonSituations":"Config expressing load factor as a percentage (75) instead of a fraction (0.75); unit-of-measure confusion after porting from libraries that accept load factors > 1; typos like 10 instead of 1.0.","solutions":["Pass a load factor in the range (0, 1]; use 0.75 as a sensible default.","Convert percentage config values: loadFactor = percentValue / 100.0.","Clamp before constructing: Math.Min(1.0, Math.Max(0.01, value))."],"exampleFix":"// before\nvar ht = new HashTable<string, int>(16, 75); // percentage, > 1\n// after\nvar ht = new HashTable<string, int>(16, 75 / 100.0); // 0.75","handlingStrategy":"validation","validationCode":"if (loadFactor > 1)\n    throw new ArgumentOutOfRangeException(nameof(loadFactor), \"Load factor must be <= 1\");","typeGuard":"static bool IsValidLoadFactor(double lf) => !double.IsNaN(lf) && lf > 0 && lf <= 1;","tryCatchPattern":"try { var ht = new HashTable<string,int>(cap, loadFactor); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"loadFactor\")\n{\n    loadFactor = Math.Min(loadFactor / 100.0, 1.0); // maybe a percentage\n}","preventionTips":["Convert percentage config values to fractions before use.","Clamp with Math.Min(1.0, value) when the source is uncertain.","Document the (0,1] range where the value is defined."],"tags":["argument-out-of-range","constructor","hash-table","validation"],"backgroundTag":"argument-out-of-range","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}