TheAlgorithms/C-Sharp · error · ArgumentException

Array is empty.

Error message

Array is empty.

What it means

Relu.Compute rejects zero-length input arrays with ArgumentException('Array is empty.') because the ReLU output of an empty vector is undefined by this API. The null case is a separate ArgumentNullException. Callers must pass at least one element.

Solutions

  1. Check input.Length > 0 before calling Compute.
  2. Decide the desired result for empty input (return empty array yourself) and bypass the call.
  3. Guard upstream filters so empty results are handled before numeric processing.

Example fix

// before
var output = Relu.Compute(filtered);
// after
var output = filtered.Length == 0 ? Array.Empty<double>() : Relu.Compute(filtered);
Defensive patterns

Strategy: validation

Validate before calling

var output = input.Length == 0 ? Array.Empty<double>() : Relu.Compute(input);

Type guard

static bool IsNonEmpty(double[]? a) => a is { Length: > 0 };

Try / catch

try { var out = Relu.Compute(input); }
catch (ArgumentException ex) when (ex.Message == "Array is empty.") { return Array.Empty<double>(); }

Prevention

When it happens

Trigger: Calling Relu.Compute(Array.Empty<double>()) or Compute(new double[0]), or Compute on an array produced by filtering that removed all elements.

Common situations: Empty CSV/data rows, a filter step (Where) yielding no items followed by ToArray, or splitting an empty string into a zero-length array.

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/caf352fa524af7c9. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Numeric/Relu.cs:34

    {
        return Math.Max(0.0, input);
    }

    /// <summary>
    ///     Compute the Rectified Linear Unit (ReLU) element-wise for a vector.
    /// </summary>
    /// <param name="input">The input vector of real numbers.</param>
    /// <returns>The output vector where each element is max(0, input[i]).</returns>
    public static double[] Compute(double[] input)
    {
        if (input is null)
        {
            throw new ArgumentNullException(nameof(input));
        }

        if (input.Length == 0)
        {
            throw new ArgumentException("Array is empty.");
        }

        var output = new double[input.Length];

        for (var i = 0; i < input.Length; i++)
        {
            output[i] = Math.Max(0.0, input[i]);
        }

        return output;
    }
}

View on GitHub (pinned to 96e2905cab)