CKTIS / C#
Func<bool> and Action<int>: signal classification and onAfterUnit
Use `Func<bool>` to test signal classification rules and `Action<int>` to notify the caller after processing a unit.
Version: C# 12 and .NET 8; later runtimes may add compatible features
Syntax
static void ProcessUnit(int unitIndex, Func<bool> isHigh, Func<bool> isLow, Action<int>? onAfterUnit = null)
{
string label = isHigh() ? "high" : isLow() ? "low" : "normal";
Console.WriteLine(label);
onAfterUnit?.Invoke(unitIndex);
}The lesson phrase Func<bool> i Action<int> means “Func<bool> and Action<int>.” Both are delegate types in System. The first takes no arguments and returns a bool; the second takes one int and returns nothing (void). If a rule should take the reading explicitly, use Func<int, bool>. The nongeneric Action takes no arguments.
In this C# 12 and .NET 8 example, ProcessUnit takes two classification delegates: isHigh and isLow. It calls isHigh() first. If that returns true, it chooses high and skips isLow(). Otherwise, isLow() determines whether the label is low or normal. The lambdas capture the reading variable created in that iteration, not a copy of its value. Keeping a delegate can extend the lifetime of the captured variable.
After printing the classification, onAfterUnit?.Invoke(unitIndex) calls the optional callback with the zero-based unit index, not the signal reading. Action<int>? allows null, = null lets the caller omit the argument, and ?. skips invocation when the delegate is null. ProcessUnit is an example method, and onAfterUnit is its parameter, not an event or a .NET API member.
Delegate invocation is synchronous. If a rule or the output operation throws, the callback is not reached; if the callback itself throws, the exception propagates to the caller. ?. does not catch exceptions. Combining delegates with += assigns a delegate with the combined invocation list without calling any methods. Invoking that delegate runs the methods in order; if one throws, the remaining methods are not called. If invocation completes normally, a combined Func<bool> returns only the last method's result, not a logical combination. Accept delegates only from trusted code: their types specify signatures, not limits on side effects.
Examples
using System;
int[] readings = { 12, -3, 4 };
Action<int> onAfterUnit = static index =>
Console.WriteLine(FormattableString.Invariant($"after unit {index}"));
for (int i = 0; i < readings.Length; i++)
{
int reading = readings[i];
Func<bool> isHigh = () => reading >= 10;
Func<bool> isLow = () => reading < 0;
ProcessUnit(i, isHigh, isLow, onAfterUnit);
}
static void ProcessUnit(int unitIndex, Func<bool> isHigh, Func<bool> isLow,
Action<int>? onAfterUnit = null)
{
string label = isHigh() ? "high" : isLow() ? "low" : "normal";
Console.WriteLine(label);
onAfterUnit?.Invoke(unitIndex);
}Result
high after unit 0 low after unit 1 normal after unit 2
For readings `12`, `-3`, and `4`, the expected labels are `high`, `low`, and `normal`, respectively. Each label is followed by an `after unit` line showing the unit index.
Related lessons
- C# · Lesson 21 · Advanced · Version: 3