{"record":{"id":"a77a3e0862aa01e2","repo":"TheAlgorithms/C-Sharp","slug":"the-lower-bound-must-be-less-than-or-equal-to-the-upper","errorCode":null,"errorMessage":"The lower bound must be less than or equal to the upper bound.","messagePattern":"The lower bound must be less than or equal to the upper bound\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Numeric/AutomorphicNumber.cs","lineNumber":33,"sourceCode":"    /// <returns>A list that contains all of the automorphic numbers between <paramref name=\"lowerBound\"/> and <paramref name=\"upperBound\"/> inclusive.</returns>\n    /// <exception cref=\"ArgumentException\">If the <paramref name=\"lowerBound\"/>\n    /// or <paramref name=\"upperBound\"/> is not greater than zero\n    /// or <paramref name=\"upperBound\"/>is lower than the <paramref name=\"lowerBound\"/>.</exception>\n    public static IEnumerable<int> GetAutomorphicNumbers(int lowerBound, int upperBound)\n    {\n        if (lowerBound < 1)\n        {\n            throw new ArgumentException($\"Lower bound must be greater than 0.\");\n        }\n\n        if (upperBound < 1)\n        {\n            throw new ArgumentException($\"Upper bound must be greater than 0.\");\n        }\n\n        if (lowerBound > upperBound)\n        {\n            throw new ArgumentException($\"The lower bound must be less than or equal to the upper bound.\");\n        }\n\n        return Enumerable.Range(lowerBound, upperBound).Where(IsAutomorphic);\n    }\n\n    /// <summary>\n    /// Checks if a given natural number is automorphic or not.\n    /// </summary>\n    /// <param name=\"number\">The number to check.</param>\n    /// <returns>True if the number is automorphic, false otherwise.</returns>\n    /// <exception cref=\"ArgumentException\">If the number is non-positive.</exception>\n    public static bool IsAutomorphic(int number)\n    {\n        if (number < 1)\n        {\n            throw new ArgumentException($\"An automorphic number must always be positive.\");\n        }\n","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Numeric/AutomorphicNumber.cs#L15-L51","documentation":"GetAutomorphicNumbers(lowerBound, upperBound) generates automorphic numbers in an inclusive-style range via Enumerable.Range. The library requires the caller to pass a lowerBound that does not exceed upperBound; passing lowerBound > upperBound would produce an empty/nonsensical range, so it throws ArgumentException with this message. This is a deliberate input-contract check, not an internal failure.","triggerScenarios":"Calling AutomorphicNumber.GetAutomorphicNumbers(a, b) where a > b, e.g. GetAutomorphicNumbers(50, 10) or a dynamically computed range where the bounds are swapped or computed in the wrong order.","commonSituations":"Passing user-supplied range inputs without ordering them first; variables accidentally swapped at the call site; off-by-one or reversed range logic when building ranges from min/max inputs.","solutions":["Ensure lowerBound <= upperBound before calling; order the inputs with Math.Min/Math.Max if the source order is unknown.","If an empty result is acceptable, skip the call when lowerBound > upperBound instead of invoking it.","Note that Enumerable.Range(lowerBound, count) uses a count, so verify upperBound is intended as a count-like bound and the range is non-degenerate."],"exampleFix":"// before\nvar result = AutomorphicNumber.GetAutomorphicNumbers(from, to); // from > to throws\n// after\nvar lower = Math.Min(from, to);\nvar upper = Math.Max(from, to);\nvar result = lower <= upper\n    ? AutomorphicNumber.GetAutomorphicNumbers(lower, upper)\n    : Enumerable.Empty<int>();","handlingStrategy":"validation","validationCode":"if (lowerBound > upperBound)\n    throw new ArgumentException(\"lowerBound must be <= upperBound\");\nvar result = AutomorphicNumber.GetAutomorphicNumbers(lowerBound, upperBound);","typeGuard":null,"tryCatchPattern":"try\n{\n    var nums = AutomorphicNumber.GetAutomorphicNumbers(lower, upper);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"lower bound\"))\n{\n    // handle swapped/invalid range\n}","preventionTips":["Normalize ranges with Math.Min/Math.Max before calling.","Validate user-supplied bounds at input time.","Remember Enumerable.Range semantics (start, count) when choosing bounds."],"tags":["argument-validation","range","csharp","algorithms"],"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"}