TheAlgorithms/C-Sharp · error · ArgumentException

Array is empty.

Error message

Array is empty.

What it means

AbsMin<T> throws ArgumentException when the input array has length 0, because there is no smallest absolute value of an empty sequence. A guard at the start of the public generic method (T : INumber<T>).

Solutions

  1. Check inputNums.Length > 0 before calling AbsMin and decide an empty-case policy (return null/default, throw your own error, or use a sentinel).
  2. Use LINQ's inputNums.OrderBy(Math.Abs).FirstOrDefault() style with an explicit empty handling if you prefer a nullable result.
  3. Ensure upstream collection code never produces empty arrays for this call path (guard at data-entry point).

Example fix

// before
var min = Abs.AbsMin(nums);
// after
var min = nums.Length == 0 ? throw new ArgumentException("nums must be non-empty") : Abs.AbsMin(nums);
Defensive patterns

Strategy: validation

Validate before calling

if (inputNums is null || inputNums.Length == 0)
    throw new ArgumentException("inputNums must contain at least one element", nameof(inputNums));

Type guard

static bool IsNonEmpty<T>(T[] arr) => arr is { Length: > 0 };

Try / catch

try { min = Abs.AbsMin(nums); }
catch (ArgumentException ex) when (ex.Message == "Array is empty.") { min = null; /* define empty policy */ }

Prevention

When it happens

Trigger: Calling AbsMin with an empty T[] — e.g. results of a filter that matched nothing, or a fresh array never populated.

Common situations: Pipelines aggregating over empty data sets; splitting/parsing inputs that yielded no tokens; callers using ToArray() on an empty LINQ query without checking Count.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/4e901c50623fcd63. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Numeric/Abs.cs:29

    /// <typeparam name="T">Type of number.</typeparam>
    /// <param name="inputNum">Number to find the absolute value of.</param>
    /// <returns>Absolute value of the number.</returns>
    public static T AbsVal<T>(T inputNum) where T : INumber<T>
    {
        return T.IsNegative(inputNum) ? -inputNum : inputNum;
    }

    /// <summary>
    ///   Returns the number with the smallest absolute value on the input array.
    /// </summary>
    /// <typeparam name="T">Type of number.</typeparam>
    /// <param name="inputNums">Array of numbers to find the smallest absolute.</param>
    /// <returns>Smallest absolute number.</returns>
    public static T AbsMin<T>(T[] inputNums) where T : INumber<T>
    {
        if (inputNums.Length == 0)
        {
            throw new ArgumentException("Array is empty.");
        }

        var min = inputNums[0];
        for (var index = 1; index < inputNums.Length; index++)
        {
            var current = inputNums[index];
            if (AbsVal(current).CompareTo(AbsVal(min)) < 0)
            {
                min = current;
            }
        }

        return min;
    }

    /// <summary>
    ///  Returns the number with the largest absolute value on the input array.
    /// </summary>

View on GitHub (pinned to 96e2905cab)