Humanizr/Humanizer · error · ArgumentOutOfRangeException

Specified argument was out of the range of valid values. (Pa

Error message

Specified argument was out of the range of valid values. (Parameter 'casing')

What it means

Thrown by ApplyCase when the LetterCasing argument is not one of the four defined values (Title, AllCaps, LowerCase, Sentence). The method uses a switch expression whose default arm throws ArgumentOutOfRangeException, so any value produced by an invalid cast or an uninitialized enum field trips it. The exception identifies the offending parameter by name ('casing').

Source

Thrown at src/Humanizer/CasingExtensions.cs:36

    /// - <see cref="LetterCasing.Sentence"/>: First character uppercased, remainder unchanged (e.g., "Some string")
    /// </returns>
    /// <exception cref="ArgumentOutOfRangeException">Thrown when an invalid <see cref="LetterCasing"/> value is provided.</exception>
    /// <example>
    /// <code>
    /// "some string".ApplyCase(LetterCasing.Title) => "Some String"
    /// "SOME STRING".ApplyCase(LetterCasing.LowerCase) => "some string"
    /// "some string".ApplyCase(LetterCasing.AllCaps) => "SOME STRING"
    /// "some string".ApplyCase(LetterCasing.Sentence) => "Some string"
    /// </code>
    /// </example>
    public static string ApplyCase(this string input, LetterCasing casing) =>
        casing switch
        {
            LetterCasing.Title => input.Transform(To.TitleCase),
            LetterCasing.LowerCase => input.Transform(To.LowerCase),
            LetterCasing.AllCaps => input.Transform(To.UpperCase),
            LetterCasing.Sentence => input.Transform(To.SentenceCase),
            _ => throw new ArgumentOutOfRangeException(nameof(casing))
        };
}

View on GitHub (pinned to ffc2b77c0f)

Solutions

  1. Validate with Enum.IsDefined(casing) before calling ApplyCase and fall back to a sensible default (e.g. LetterCasing.Title).
  2. Constrain input at the boundary: parse strings with Enum.TryParse<LetterCasing> and reject unknown values rather than casting raw ints.
  3. If the invalid value originates upstream, fix the source (serializer config, database value, or enum definition) so only defined members reach ApplyCase.

Example fix

// before
var result = myString.ApplyCase((LetterCasing)userChoice);

// after
if (!Enum.IsDefined(typeof(LetterCasing), userChoice))
    throw new ArgumentOutOfRangeException(nameof(userChoice));
var result = myString.ApplyCase((LetterCasing)userChoice);
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(LetterCasing), casing))
    throw new ArgumentOutOfRangeException(nameof(casing));
var result = input.ApplyCase(casing);

Type guard

static bool IsValidLetterCasing(LetterCasing casing) => Enum.IsDefined(typeof(LetterCasing), casing);

Try / catch

try { result = input.ApplyCase(casing); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "casing")
{ /* log and fall back to a defined casing */ result = input.ApplyCase(LetterCasing.Title); }

Prevention

When it happens

Trigger: Calling input.ApplyCase((LetterCasing)999), input.ApplyCase(default(LetterCasing)) is valid (Title=0) so that does NOT throw, but casting an arbitrary int outside [0..3] does. Indirectly hit when Humanize<T>(input, casing) receives an invalid LetterCasing, since that overload forwards to ApplyCase after its own Enum.IsDefined check.

Common situations: Deserializing a LetterCasing value from JSON/config where the numeric backing value is out of range; passing an enum parsed from unvalidated user input; storing LetterCasing in a database column as an int and later casting without validation; threading a 'style' int from another enum into ApplyCase.

Related errors


AI-assisted analysis of Humanizr/Humanizer@ffc2b77c0f (2026-08-13). Data as JSON: /api/errors/ad7f13256ddacabd. Report an issue: GitHub.