functional programming principles applied in C#
Actions are sugar syntax to create delegates that the .NET framework
gives us. These never return a value therefore are always void
, and can
take up to sixteen generic parameters as input.
void
return type in FP is called unit
unit
is expressed as ()
in arrow notationusing System;
using System.Diagnostics;
internal static class TestActions
{
internal static void Run()
{
// string -> ()
Action<string> consoleLoggerHandler = ConsoleLogger;
Action<string> debugLoggerHandler = DebugLogger;
Action<string> allConsoleHandlers = consoleLoggerHandler + debugLoggerHandler;
consoleLoggerHandler("This goes to the console");
debugLoggerHandler("This goes to the debug");
allConsoleHandlers("this goes to all");
}
private static void ConsoleLogger(string message) => Console.WriteLine(message);
private static void DebugLogger(string message) => Debug.WriteLine(message);
}