CKTIS / C#
Func delegates and invariant report formatting
Use a bool-returning delegate to filter items, a decimal-returning delegate to select amounts, and invariant formatting to render the total.
Version: C# 12 and .NET 8; later runtimes may add compatible features
Syntax
Func<Item, bool> predicate = item => item.Included;
Func<Item, decimal> selector = item => item.Amount;
decimal total = items.Where(predicate).Sum(selector);
FormattableString report = $"Total={total:F2}";
string text = FormattableString.Invariant(report);The scope phrase Func<Item,bool>/Func<Item,decimal> and FormattableString.Invariant pairs two delegate types with a formatting method. System.Func<T, TResult> takes one T and returns a TResult. With the example's Item type, Func<Item, bool> is a predicate: true means keep the item. Func<Item, decimal> is a selector: it returns the item's amount. Both can be assigned lambdas written with =>, but the types aren't interchangeable.
In System.Linq, the non-indexed Enumerable.Where overload takes the predicate and defers filtering until enumeration. Sum(selector) enumerates that sequence and adds the amounts for matching items, using the overload that returns decimal, not decimal?. No matches produce zero; overflow throws OverflowException.
An interpolation assigned to System.FormattableString retains its format and arguments. ToString() uses the current culture; ToString(IFormatProvider) uses the specified provider. The static FormattableString.Invariant method takes a FormattableString, returns a string formatted with the invariant culture, and throws ArgumentNullException for null. Pass the interpolation directly or keep it as a FormattableString; the method can't reformat an already rendered string.
F2 displays two fractional digits without grouping and doesn't change the decimal value. Use the user's culture for localized display and invariant culture for a report that requires a fixed decimal separator. Invariant formatting doesn't replace serialization or the escaping required by the target format.
Examples
using System;
using System.Globalization;
using System.Linq;
Item[] items =
[
new Item(true, 12.50m),
new Item(false, 99.00m),
new Item(true, 7.90m)
];
Func<Item, bool> predicate = item => item.Included;
Func<Item, decimal> selector = item => item.Amount;
decimal total = items.Where(predicate).Sum(selector);
FormattableString report = $"Total={total:F2}";
var customCulture = (CultureInfo)CultureInfo.InvariantCulture.Clone();
customCulture.NumberFormat.NumberDecimalSeparator = ",";
Console.WriteLine("Custom: " + report.ToString(customCulture));
Console.WriteLine("Invariant: " + FormattableString.Invariant(report));
internal sealed record Item(bool Included, decimal Amount);
Result
Custom: Total=20,40 Invariant: Total=20.40
The `99.00` item is excluded. The same total uses a comma with an explicitly supplied, modified copy of the invariant culture and a dot with `Invariant`. The program doesn't change the current culture.
Related lessons
- C# · Lesson 25 · Advanced · Version: 3
Sources
- Func<T,TResult> Delegate (System)
- Enumerable.Where Method (System.Linq)
- Enumerable.Sum Method (System.Linq)
- FormattableString.Invariant(FormattableString) Method (System)
- FormattableString.ToString Method (System)
- $ - string interpolation - format string output - C# reference
- Standard numeric format strings - .NET
- Best practices for displaying and persisting formatted data in .NET