CKTIS / C#
Func delegates for calculating and formatting a price
Use one delegate to calculate a decimal amount from a product and another to turn that amount into text with an explicit format.
Version: C# 12 and .NET 8; later runtimes may add compatible features
Syntax
System.Func<Product,decimal> strategy = static p => decimal.Round(p.Price, 2, System.MidpointRounding.AwayFromZero);
System.Func<decimal,string> fmt = static amount => amount.ToString("F2", System.Globalization.CultureInfo.InvariantCulture);
string text = fmt(strategy(product));System.Func<T,TResult> is a delegate type in the System namespace: T is the type of its single argument, and TResult is its return type. System.Func<Product,decimal> is the type of the pricing delegate strategy, which takes a Product and returns a decimal. Product is defined in the example, and decimal is the C# alias for System.Decimal. System.Func<decimal,string> is the type of the formatter delegate fmt, which takes a decimal and returns a string.
In these lambdas, the parameter is to the left of => and the expression that produces the result is to the right. Assigning a lambda does not calculate the price: the code runs when the delegate is invoked. A static lambda does not capture surrounding local variables. In fmt(strategy(product)), the strategy runs first and its result goes to the formatter. You can replace either delegate independently with one that has the same signature.
decimal.Round(decimal, int, MidpointRounding) returns a numeric value rounded to the specified number of decimal places (0 to 28). MidpointRounding.AwayFromZero chooses the nearest value; at an exact tie, it chooses the one farther from zero. The decimal.Round(decimal, int) overload uses ToEven for ties, choosing the result with an even last retained digit.
ToString("F2", CultureInfo.InvariantCulture) produces text with exactly two digits after a decimal point, regardless of the current culture. Formatting may round the displayed digits, but it does not change the decimal value or replace the strategy's rounding rule.
Examples
using System;
using System.Globalization;
var product = new Product(12.345m);
Func<Product, decimal> strategy = static p =>
decimal.Round(p.Price, 2, MidpointRounding.AwayFromZero);
Func<decimal, string> fmt = static amount =>
amount.ToString("F2", CultureInfo.InvariantCulture);
decimal price = strategy(product);
Console.WriteLine(fmt(price));
sealed record Product(decimal Price);Result
12.35
The strategy rounds `12.345m` to `12.35m`, and `fmt` turns the result into the text `12.35`.
Related lessons
- C# · Lesson 27 · Intermediate · Version: 2
- C# · Lesson 44 · Intermediate · Version: 2
Sources
- Func<T,TResult> Delegate (System)
- Lambda expressions - Lambda expressions and anonymous functions - C# reference
- Decimal.Round Method (System)
- MidpointRounding Enum (System)
- Decimal.ToString Method (System)
- Standard numeric format strings - .NET
- CultureInfo.InvariantCulture Property (System.Globalization)