{"record":{"id":"df0de7b82b799c9c","repo":"TheAlgorithms/C-Sharp","slug":"cash-flows-list-cannot-be-empty","errorCode":null,"errorMessage":"Cash flows list cannot be empty","messagePattern":"Cash flows list cannot be empty","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Financial/PresentValue.cs","lineNumber":17,"sourceCode":"namespace Algorithms.Financial;\n\n/// <summary>\n/// PresentValue is the value of an expected income stream determined as of the date of valuation.\n/// </summary>\npublic static class PresentValue\n{\n    public static double Calculate(double discountRate, List<double> cashFlows)\n    {\n        if (discountRate < 0)\n        {\n            throw new ArgumentException(\"Discount rate cannot be negative\");\n        }\n\n        if (cashFlows.Count == 0)\n        {\n            throw new ArgumentException(\"Cash flows list cannot be empty\");\n        }\n\n        double presentValue = cashFlows.Select((t, i) => t / Math.Pow(1 + discountRate, i)).Sum();\n\n        return Math.Round(presentValue, 2);\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":25,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Financial/PresentValue.cs#L1-L25","documentation":"Calculate in Algorithms/Financial/PresentValue.cs throws ArgumentException(\"Cash flows list cannot be empty\") when the cashFlows collection has zero entries (checked via cashFlows.Count == 0 at line 17). The present-value computation sums each cash flow discounted by period, and with no flows the result is mathematically undefined/meaningless, so the library rejects the input up front rather than returning 0. It is a caller-input validation error, not an internal failure.","triggerScenarios":"Calling PresentValue.Calculate(discountRate, cashFlows) with an empty list/array (Count == 0). Note that a non-negative discountRate is validated first; an empty cashFlows list is the second validation. Any call where the caller builds the flow list dynamically (e.g. from a filtered stream or empty user input) can end up with zero elements.","commonSituations":"Filtering cash flows by date range that matches nothing; a spreadsheet/DB import returning no rows; passing a default-initialized List<double> that was never populated; user submits a valuation form with no payment entries.","solutions":["Populate the cashFlows list with at least one cash flow before calling Calculate.","Check cashFlows.Count > 0 (or cashFlows.Any()) in the caller and handle the empty case explicitly (return 0, show a validation message, etc.).","If flows come from a data source, debug why the source returned zero rows before treating this as a calculation error."],"exampleFix":"// before\nvar flows = payments.Where(p => p.Year > cutoffYear).Select(p => p.Amount).ToList();\nvar pv = PresentValue.Calculate(rate, flows); // throws if filter matched nothing\n\n// after\nvar flows = payments.Where(p => p.Year > cutoffYear).Select(p => p.Amount).ToList();\nif (flows.Count == 0)\n{\n    Console.WriteLine(\"No cash flows in range; PV is undefined.\");\n    return 0;\n}\nvar pv = PresentValue.Calculate(rate, flows);","handlingStrategy":"validation","validationCode":"if (cashFlows == null || cashFlows.Count == 0)\n    throw new ArgumentException(\"At least one cash flow is required before computing present value.\");\nif (discountRate < 0)\n    throw new ArgumentException(\"Discount rate must be non-negative.\");","typeGuard":"bool HasCashFlows(IEnumerable<double>? flows) => flows != null && flows.Any();","tryCatchPattern":"try\n{\n    var pv = PresentValue.Calculate(rate, cashFlows);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Cash flows list cannot be empty\"))\n{\n    // treat as user-input validation problem\n}","preventionTips":["Validate cash flow lists with .Any() immediately after building them from filters or imports.","Return an empty-list error from your own data-access layer instead of silently passing empty collections onward.","Add unit tests covering the empty-flow case for any valuation wrapper you write."],"tags":["csharp","finance","argument-validation"],"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"}