Review generated order summary

from C# fundamentals
C# 14 / .NET 10 intermediate 6 min 4 issues to find

Review this generated order helper before it handles customer-controlled quantities.

Accept an array of quantity strings, ignore malformed or nonpositive values, return an exact total without overflow, and load a customer name at most once.

csharp
using System;

public static class OrderSummary
{
    public static int CountUnits(string[] values)
    {
        int total = 0;
        for (int index = 0; index <= values.Length; index++)
        {
            int quantity = int.Parse(values[index]);
            if (quantity < 0) continue;
            total += quantity;
        }
        return total;
    }

    public static string CustomerName(Func<string?> load)
    {
        return load() is null ? "guest" : load()!;
    }
}

generated code is illustrative, not from any one model

Open in playground
Report an error