{"record":{"id":"ed30b358f084b08a","repo":"TheAlgorithms/C-Sharp","slug":"nameof-number-cannot-be-negative","errorCode":null,"errorMessage":"{nameof(number)} cannot be negative","messagePattern":"(.+?) cannot be negative","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Numeric/AliquotSumCalculator.cs","lineNumber":21,"sourceCode":"/// <summary>\n///     In number theory, the aliquot sum s(n) of a positive integer n is the sum of all proper divisors\n///     of n, that is, all divisors of n other than n itself. For example, the proper divisors of 15\n///     (that is, the positive divisors of 15 that are not equal to 15) are 1, 3 and 5, so the aliquot\n///     sum of 15 is 9 i.e. (1 + 3 + 5). Wikipedia: https://en.wikipedia.org/wiki/Aliquot_sum.\n/// </summary>\npublic static class AliquotSumCalculator\n{\n    /// <summary>\n    ///     Finds the aliquot sum of an integer number.\n    /// </summary>\n    /// <param name=\"number\">Positive number.</param>\n    /// <returns>The Aliquot Sum.</returns>\n    /// <exception cref=\"ArgumentException\">Error number is not on interval (0.0; int.MaxValue).</exception>\n    public static int CalculateAliquotSum(int number)\n    {\n        if (number < 0)\n        {\n            throw new ArgumentException($\"{nameof(number)} cannot be negative\");\n        }\n\n        var sum = 0;\n        for (int i = 1, limit = number / 2; i <= limit; ++i)\n        {\n            if (number % i == 0)\n            {\n                sum += i;\n            }\n        }\n\n        return sum;\n    }\n}\n","sourceCodeStart":3,"sourceCodeEnd":36,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Numeric/AliquotSumCalculator.cs#L3-L36","documentation":"CalculateAliquotSum throws ArgumentException for negative inputs. The aliquot sum (sum of proper divisors) is defined for non-negative integers, and the divisor loop assumes number >= 0; the XML doc notes the valid interval (0; int.MaxValue).","triggerScenarios":"Calling AliquotSumCalculator.CalculateAliquotSum with a negative int, e.g. from user input or subtraction (a - b) that went below zero.","commonSituations":"Perfect/abundant-number classification over datasets containing negatives; sign mistakes when computing inputs; unvalidated user or config values.","solutions":["Validate number >= 0 before calling and reject or clamp negatives in your own code.","Decide domain semantics: 0 is allowed by the code (returns 0 per loop bounds), negatives are not — filter them out upstream.","Wrap in try-catch for ArgumentException only as a last line of defense."],"exampleFix":"// before\nvar sum = AliquotSumCalculator.CalculateAliquotSum(n);\n// after\nvar sum = n >= 0 ? AliquotSumCalculator.CalculateAliquotSum(n) : throw new ArgumentOutOfRangeException(nameof(n));","handlingStrategy":"validation","validationCode":"if (number < 0) throw new ArgumentOutOfRangeException(nameof(number), \"aliquot sum requires a non-negative integer\");","typeGuard":"static bool IsValidAliquotInput(int n) => n >= 0;","tryCatchPattern":"try { sum = AliquotSumCalculator.CalculateAliquotSum(n); }\ncatch (ArgumentException ex) { /* reject or clamp negative input */ }","preventionTips":["Validate user/config numbers at intake.","Filter datasets to non-negative values before number-theory operations.","Note the documented domain (0; int.MaxValue) and encode it in your own API."],"tags":["csharp","math","number-theory","argument-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"}