{"record":{"id":"3f74cdea57ac4c88","repo":"dotnet/csharplang","slug":"argumentexpression-argument-cannot-be-less-t","errorCode":null,"errorMessage":"{argumentExpression} ({argument}) cannot be less than {lowExpression} ({low}).","messagePattern":"(.+?) \\((.+?)\\) cannot be less than (.+?) \\((.+?)\\)\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"proposals/csharp-10.0/caller-argument-expression.md","lineNumber":95,"sourceCode":"\nFor argument validation, the attribute cannot be used directly, but can be made use of through a helper class:\n\n```csharp\npublic static class Verify\n{\n    public static void Argument(bool condition, string message, [CallerArgumentExpression(\"condition\")] string conditionExpression = null)\n    {\n        if (!condition) throw new ArgumentException(message: message, paramName: conditionExpression);\n    }\n\n    public static void InRange(int argument, int low, int high,\n        [CallerArgumentExpression(\"argument\")] string argumentExpression = null,\n        [CallerArgumentExpression(\"low\")] string lowExpression = null,\n        [CallerArgumentExpression(\"high\")] string highExpression = null)\n    {\n        if (argument < low)\n        {\n            throw new ArgumentOutOfRangeException(paramName: argumentExpression,\n                message: $\"{argumentExpression} ({argument}) cannot be less than {lowExpression} ({low}).\");\n        }\n\n        if (argument > high)\n        {\n            throw new ArgumentOutOfRangeException(paramName: argumentExpression,\n                message: $\"{argumentExpression} ({argument}) cannot be greater than {highExpression} ({high}).\");\n        }\n    }\n\n    public static void NotNull<T>(T argument, [CallerArgumentExpression(\"argument\")] string argumentExpression = null)\n        where T : class\n    {\n        if (argument == null) throw new ArgumentNullException(paramName: argumentExpression);\n    }\n}\n\nstatic T Single<T>(this T[] array)","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/dotnet/csharplang/blob/05eb4800fc3dc76259ccd49ac01f1eeb49222380/proposals/csharp-10.0/caller-argument-expression.md#L77-L113","documentation":"Thrown by an example `InRange(int argument, int low, int high)` helper from the caller-argument-expression proposal when `argument < low`. The message interpolates the CallerArgumentExpression-captured source text of each parameter (e.g. `argumentExpression`), the runtime value, and the bound, so the developer sees both the expression and the value that violated it.","triggerScenarios":"Calling `InRange(x, low, high)` with `x < low`. Passing a computed offset, count, or index that can dip below a documented minimum (often 0).","commonSituations":"Negative inputs where 0 is the floor; off-by-one in loops/size calculations; user-supplied configuration values (page size, retry count) clamped too loosely; deserialized data that skips UI-side bounds checks.","solutions":["Validate the value is within `[low, high]` before calling InRange, and surface a clearer domain error to the caller if not.","Clamp the input to the valid range: `argument = Math.Max(low, Math.Min(high, argument));` when a clamped fallback is acceptable.","Correct the producer of the value (parse/format/config layer) so it cannot emit below-range numbers.","Treat the lower bound as inclusive and adjust the caller's arithmetic if the boundary was meant to be exclusive."],"exampleFix":"// before\nGuard.InRange(count, 0, maxItems);\n\n// after\nint clamped = Math.Clamp(count, 0, maxItems);\nGuard.InRange(clamped, 0, maxItems);","handlingStrategy":"validation","validationCode":"static void EnsureInRange(int argument, int low, int high)\n{\n    if (argument < low || argument > high)\n        throw new ArgumentOutOfRangeException(nameof(argument),\n            $\"{argument} is outside [{low}, {high}].\");\n}","typeGuard":"static bool IsWithin(int value, int low, int high) => value >= low && value <= high;","tryCatchPattern":"try { Guard.InRange(value, low, high); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == nameof(value))\n{\n    // clamp or report the bound violation\n}","preventionTips":["Validate ranges at the trust boundary (input/config) rather than deep in call chains.","Use Math.Clamp for fallback values where truncation is acceptable.","Document inclusive vs exclusive bounds and write tests at each boundary."],"tags":["range","argument-validation","int","csharp"],"backgroundTag":null,"analyzedSha":"05eb4800fc3dc76259ccd49ac01f1eeb49222380","analyzedAt":"2026-08-13T17:44:03.908Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}