TheAlgorithms/C-Sharp · error · ArgumentException
Cash flows list cannot be empty
Error message
Cash flows list cannot be empty
What it means
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.
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.
Example fix
// before
var flows = payments.Where(p => p.Year > cutoffYear).Select(p => p.Amount).ToList();
var pv = PresentValue.Calculate(rate, flows); // throws if filter matched nothing
// after
var flows = payments.Where(p => p.Year > cutoffYear).Select(p => p.Amount).ToList();
if (flows.Count == 0)
{
Console.WriteLine("No cash flows in range; PV is undefined.");
return 0;
}
var pv = PresentValue.Calculate(rate, flows); Defensive patterns
Strategy: validation
Validate before calling
if (cashFlows == null || cashFlows.Count == 0)
throw new ArgumentException("At least one cash flow is required before computing present value.");
if (discountRate < 0)
throw new ArgumentException("Discount rate must be non-negative."); Type guard
bool HasCashFlows(IEnumerable<double>? flows) => flows != null && flows.Any();
Try / catch
try
{
var pv = PresentValue.Calculate(rate, cashFlows);
}
catch (ArgumentException ex) when (ex.Message.Contains("Cash flows list cannot be empty"))
{
// treat as user-input validation problem
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
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
- Invalid parameter settings for Ascon Hash
- Discount rate cannot be negative
- cannot be negative
- The lower bound must be less than or equal to the upper…
- An automorphic number must always be positive.
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/df0de7b82b799c9c.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Financial/PresentValue.cs:17
namespace Algorithms.Financial;
/// <summary>
/// PresentValue is the value of an expected income stream determined as of the date of valuation.
/// </summary>
public static class PresentValue
{
public static double Calculate(double discountRate, List<double> cashFlows)
{
if (discountRate < 0)
{
throw new ArgumentException("Discount rate cannot be negative");
}
if (cashFlows.Count == 0)
{
throw new ArgumentException("Cash flows list cannot be empty");
}
double presentValue = cashFlows.Select((t, i) => t / Math.Pow(1 + discountRate, i)).Sum();
return Math.Round(presentValue, 2);
}
}
View on GitHub (pinned to 96e2905cab)