{"record":{"id":"65dc6e40a0360010","repo":"TheAlgorithms/C-Sharp","slug":"capacity-must-be-greater-than-0","errorCode":null,"errorMessage":"Capacity must be greater than 0","messagePattern":"Capacity must be greater than 0","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"DataStructures/Hashing/HashTable.cs","lineNumber":95,"sourceCode":"\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"HashTable{TKey, TValue}\"/> class.\n    /// </summary>\n    /// <param name=\"capacity\">Initial capacity of the hash table.</param>\n    /// <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","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Hashing/HashTable.cs#L77-L113","documentation":"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).","triggerScenarios":"new HashTable<K,V>(capacity: 0), new HashTable<K,V>(-5), or a capacity computed from input/config that is zero or negative.","commonSituations":"Config files with unset numeric fields defaulting to 0, computing capacity as expectedCount - removed where removed >= expectedCount, or passing 0 intending 'use default'.","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)."],"exampleFix":"// before\nvar capacity = config.ExpectedEntries ?? 0; // 0 -> throws\nvar table = new HashTable<string, int>(capacity);\n// after\nvar capacity = Math.Max(1, config.ExpectedEntries ?? DefaultCapacity);\nvar table = new HashTable<string, int>(capacity);","handlingStrategy":"validation","validationCode":"static bool IsValidCapacity(int capacity) => capacity > 0;\n// call: if (!IsValidCapacity(cap)) cap = DefaultCapacity;","typeGuard":"static bool IsPositive(int v) => v > 0;","tryCatchPattern":"try { var t = new HashTable<K,V>(cap, lf); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"capacity\") { var t = new HashTable<K,V>(); }","preventionTips":["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."],"tags":["csharp","hashtable","constructor","argument-out-of-range"],"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"}