{"record":{"id":"8b32815a15d78946","repo":"TheAlgorithms/C-Sharp","slug":"load-factor-must-be-greater-than-0","errorCode":null,"errorMessage":"Load factor must be greater than 0","messagePattern":"Load factor must be greater than 0","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"DataStructures/Hashing/HashTable.cs","lineNumber":100,"sourceCode":"    /// <param name=\"loadFactor\">Load factor of the hash table.</param>\n    /// <exception cref=\"ArgumentOutOfRangeException\">Thrown when <paramref name=\"capacity\"/> is less than or equal to 0.</exception>\n    /// <exception cref=\"ArgumentOutOfRangeException\">Thrown when <paramref name=\"loadFactor\"/> is less than or equal to 0.</exception>\n    /// <exception cref=\"ArgumentOutOfRangeException\">Thrown when <paramref name=\"loadFactor\"/> is greater than 1.</exception>\n    /// <remarks>\n    /// <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>","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Hashing/HashTable.cs#L82-L118","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar ht = new HashTable<string, int>(16, config.LoadFactor); // LoadFactor may be 0\n// after\nvar lf = config.LoadFactor > 0 && config.LoadFactor <= 1 ? config.LoadFactor : 0.75;\nvar ht = new HashTable<string, int>(16, lf);","handlingStrategy":"validation","validationCode":"if (double.IsNaN(loadFactor) || loadFactor <= 0 || loadFactor > 1)\n    throw new ArgumentOutOfRangeException(nameof(loadFactor), \"Load factor must be in (0, 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 = 0.75; // fallback default\n}","preventionTips":["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."],"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"}