{"record":{"id":"4bf4fc86c0fd7e20","repo":"TheAlgorithms/C-Sharp","slug":"nameof-number-cannot-be-negative-perfectnumberchecker","errorCode":null,"errorMessage":"{nameof(number)} cannot be negative","messagePattern":"(.+?) cannot be negative","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Numeric/PerfectNumberChecker.cs","lineNumber":20,"sourceCode":"\n/// <summary>\n///     In number theory, a perfect number is a positive integer that is equal to the sum of its positive\n///     divisors, excluding the number itself.For instance, 6 has divisors 1, 2 and 3 (excluding\n///     itself), and 1 + 2 + 3 = 6, so 6 is a perfect number.\n/// </summary>\npublic static class PerfectNumberChecker\n{\n    /// <summary>\n    ///     Checks if a number is a perfect number or not.\n    /// </summary>\n    /// <param name=\"number\">Number to check.</param>\n    /// <returns>True if is a perfect number; False otherwise.</returns>\n    /// <exception cref=\"ArgumentException\">Error number is not on interval (0.0; int.MaxValue).</exception>\n    public static bool IsPerfectNumber(int number)\n    {\n        if (number < 0)\n        {\n            throw new ArgumentException($\"{nameof(number)} cannot be negative\");\n        }\n\n        var sum = 0; /* sum of its positive divisors */\n        for (var i = 1; i < number; ++i)\n        {\n            if (number % i == 0)\n            {\n                sum += i;\n            }\n        }\n\n        return sum == number;\n    }\n}\n","sourceCodeStart":2,"sourceCodeEnd":35,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Numeric/PerfectNumberChecker.cs#L2-L35","documentation":"IsPerfectNumber(number) sums the proper divisors of a non-negative integer and compares to the number itself. Negative numbers are outside the definition domain, so the library throws ArgumentException naming the parameter. Note 0 passes the guard but the divisor loop yields sum 0, so 0 is trivially treated as 'perfect' by this implementation.","triggerScenarios":"Calling PerfectNumberChecker.IsPerfectNumber with a negative int, e.g. IsPerfectNumber(-6).","commonSituations":"Scanning a signed integer range that crosses zero, or subtracting an offset that pushes values below 0.","solutions":["Validate number >= 0 (ideally >= 1) before calling.","Restrict range scans to positive integers.","Use Math.Abs if sign is irrelevant, though mathematically only positive numbers are perfect."],"exampleFix":"// before\nforeach (var n in numbers) IsPerfectNumber(n); // may include negatives\n// after\nforeach (var n in numbers.Where(n => n > 0)) IsPerfectNumber(n);","handlingStrategy":"validation","validationCode":"bool isPerfect = number > 0 && PerfectNumberChecker.IsPerfectNumber(number);","typeGuard":"static bool IsPositive(int v) => v > 0;","tryCatchPattern":"try { return PerfectNumberChecker.IsPerfectNumber(n); }\ncatch (ArgumentException) { return false; // negatives are never perfect }","preventionTips":["Restrict scans to positive integers (perfect numbers are positive by definition)","Check range boundaries that can dip below zero","Use uint for number-theory inputs where feasible"],"tags":["argument-validation","negative-number","csharp"],"backgroundTag":"invalid-argument-value","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"}