{"record":{"id":"4e901c50623fcd63","repo":"TheAlgorithms/C-Sharp","slug":"array-is-empty","errorCode":null,"errorMessage":"Array is empty.","messagePattern":"Array is empty\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Numeric/Abs.cs","lineNumber":29,"sourceCode":"    /// <typeparam name=\"T\">Type of number.</typeparam>\n    /// <param name=\"inputNum\">Number to find the absolute value of.</param>\n    /// <returns>Absolute value of the number.</returns>\n    public static T AbsVal<T>(T inputNum) where T : INumber<T>\n    {\n        return T.IsNegative(inputNum) ? -inputNum : inputNum;\n    }\n\n    /// <summary>\n    ///   Returns the number with the smallest absolute value on the input array.\n    /// </summary>\n    /// <typeparam name=\"T\">Type of number.</typeparam>\n    /// <param name=\"inputNums\">Array of numbers to find the smallest absolute.</param>\n    /// <returns>Smallest absolute number.</returns>\n    public static T AbsMin<T>(T[] inputNums) where T : INumber<T>\n    {\n        if (inputNums.Length == 0)\n        {\n            throw new ArgumentException(\"Array is empty.\");\n        }\n\n        var min = inputNums[0];\n        for (var index = 1; index < inputNums.Length; index++)\n        {\n            var current = inputNums[index];\n            if (AbsVal(current).CompareTo(AbsVal(min)) < 0)\n            {\n                min = current;\n            }\n        }\n\n        return min;\n    }\n\n    /// <summary>\n    ///  Returns the number with the largest absolute value on the input array.\n    /// </summary>","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Numeric/Abs.cs#L11-L47","documentation":"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>).","triggerScenarios":"Calling AbsMin with an empty T[] — e.g. results of a filter that matched nothing, or a fresh array never populated.","commonSituations":"Pipelines aggregating over empty data sets; splitting/parsing inputs that yielded no tokens; callers using ToArray() on an empty LINQ query without checking Count.","solutions":["Check inputNums.Length > 0 before calling AbsMin and decide an empty-case policy (return null/default, throw your own error, or use a sentinel).","Use LINQ's inputNums.OrderBy(Math.Abs).FirstOrDefault() style with an explicit empty handling if you prefer a nullable result.","Ensure upstream collection code never produces empty arrays for this call path (guard at data-entry point)."],"exampleFix":"// before\nvar min = Abs.AbsMin(nums);\n// after\nvar min = nums.Length == 0 ? throw new ArgumentException(\"nums must be non-empty\") : Abs.AbsMin(nums);","handlingStrategy":"validation","validationCode":"if (inputNums is null || inputNums.Length == 0)\n    throw new ArgumentException(\"inputNums must contain at least one element\", nameof(inputNums));","typeGuard":"static bool IsNonEmpty<T>(T[] arr) => arr is { Length: > 0 };","tryCatchPattern":"try { min = Abs.AbsMin(nums); }\ncatch (ArgumentException ex) when (ex.Message == \"Array is empty.\") { min = null; /* define empty policy */ }","preventionTips":["Check array length before aggregating.","Avoid ToArray() on possibly-empty LINQ queries without a length check.","Return early on empty input at the pipeline stage that owns the data."],"tags":["csharp","math","empty-collection","generics"],"backgroundTag":"empty-required-field","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"}