Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

System.Func and FormattableString.Invariant: closures and rounding

Bind an argument to a delegate with partial application, see what a closure captures, and keep decimal rounding separate from culture-invariant formatting.

Version: C# 12 and .NET 8; later runtimes may add compatible features

Syntax

Func<decimal, int, decimal> round = static (value, digits) => Math.Round(value, digits, MidpointRounding.ToEven);
static Func<decimal, decimal> BindDigits(Func<decimal, int, decimal> operation, int digits) => value => operation(value, digits);
string text = FormattableString.Invariant($"{round(1.245m, 2):F2}");

In C# 12 and .NET 8, System.Func is a family of delegate types that return a value. The last type argument is the result type; the preceding type arguments are the parameter types in call order. Func<TResult> takes no parameters, Func<T, TResult> takes one, and Func<T1, T2, TResult> takes two. Use f(x) to invoke a one-parameter delegate; assigning a lambda does not run it.

Partial application fixes some arguments and returns a function that accepts the rest. C# does not do this automatically: BindDigits returns a lambda that captures its operation and digits parameters. The lambda and its captured variables form a closure. Capture keeps a variable alive beyond its lexical scope and does not take a snapshot of its value. However, BindDigits receives its own digits parameter by value, so reassigning the caller’s digits does not affect fixedRound. In contrast, liveRound captures the caller’s variable directly and reads its current value. A static lambda cannot capture local variables or instance state.

System.Math.Round(decimal, int, MidpointRounding) returns a decimal. With MidpointRounding.ToEven, it chooses the nearest value at the requested precision. At an exact midpoint it chooses the one whose last retained digit is even: to two places, 1.245m becomes 1.24m and 1.255m becomes 1.26m. Math.Round(decimal, int) uses that same rule by default. The m suffix makes these decimal literals, not double literals, whose binary representation can affect midpoint detection. A digit count outside 0–28 throws ArgumentOutOfRangeException; an invalid mode value throws ArgumentException.

The static method System.FormattableString.Invariant(FormattableString) returns a string formatted with CultureInfo.InvariantCulture; a null argument throws ArgumentNullException. Pass $"…" directly or assign it to FormattableString first: the expressions are evaluated when the object is created, and it retains the format template and evaluated arguments. Assigning the interpolation to string first normally formats it using the current culture and does not retain the template or arguments for reformatting. Here, F2 displays two fractional digits; it does not replace the rounding rule, provide safe escaping, or define a complete serialization format.

Examples

using System;

Func<decimal, int, decimal> round = static (value, digits) =>
    Math.Round(value, digits, MidpointRounding.ToEven);

int digits = 2;
Func<decimal, decimal> fixedRound = BindDigits(round, digits);
Func<decimal, decimal> liveRound = value => round(value, digits);

digits = 1;
Console.WriteLine(FormattableString.Invariant(
    $"fixed={fixedRound(1.245m):F2}; {fixedRound(1.255m):F2}"));
Console.WriteLine(FormattableString.Invariant($"live={liveRound(1.245m):F2}"));

static Func<decimal, decimal> BindDigits(
    Func<decimal, int, decimal> operation, int digits) =>
    value => operation(value, digits);

Result

fixed=1.24; 1.26
live=1.20

`fixedRound` still rounds to two places, while `liveRound` now rounds to one. `F2` displays `1.20` even though `liveRound` returns `1.2m`; invariant formatting supplies the decimal point.

Related lessons

  • C# · Lesson 49 · Advanced · Version: 3

Sources