Func and Action in C#

Func and Action are generic delegates provided by .NET to simplify delegate usage. They save you from having to define custom delegate types for common scenarios.


1. Func Delegate

  • Purpose: Represents a method that takes input parameters and returns a value.
  • Signature: Func<T1, T2, ..., TResult>
    • T1, T2, ...: Input parameter types.
    • TResult: Return type.

Examples of Func Usage

  1. Basic Usage
Func<int, int, int> add = (x, y) => x + y; 
Console.WriteLine(add(5, 3)); 
// Output: 8
  1. Single Input, Single Output
Func<int, string> convertToString = (number) => $"Number: {number}"; 
Console.WriteLine(convertToString(42)); 
// Output: Number: 42
  1. No Parameters
Func<string> getGreeting = () => "Hello, Func!"; 
Console.WriteLine(getGreeting()); 
// Output: Hello, Func!
  1. Using Func as a Parameter
public static void PerformOperation(int a, int b, Func<int, int, int> operation)
{ 
  Console.WriteLine($"Result: {operation(a, b)}");
} 
PerformOperation(10, 5, (x, y) => x - y); 
// Output: Result: 5

2. Action Delegate

  • Purpose: Represents a method that takes input parameters but does not return a value.
  • Signature: Action<T1, T2, ...>
    • T1, T2, ...: Input parameter types.

Examples of Action Usage

  1. Basic Usage
Action<string> printMessage = (message) => Console.WriteLine(message); 
printMessage("Hello, Action!"); 
// Output: Hello, Action!
  1. Multiple Parameters
Action<int, int> printSum = (x, y) => Console.WriteLine($"Sum: {x + y}"); 
printSum(3, 4); 
// Output: Sum: 7
  1. No Parameters
Action printHello = () => Console.WriteLine("Hello, World!"); 
printHello(); 
// Output: Hello, World!
  1. Using Action as a Parameter
public static void ProcessAction(Action<string> action, string value) 
{ 
action(value); 
} 
ProcessAction((msg) => Console.WriteLine($"Processed: {msg}"), "Action Test"); 
// Output: Processed: Action Test

Comparison of Func and Action

FeatureFuncAction
PurposeFor methods that return a valueFor methods with no return value
Return TypeHas a return type (TResult)Always void
ParametersCan have zero or more input parametersCan have zero or more input parameters
ExampleFunc<int, int, int> (x, y) => x + yAction<int> (x) => Console.WriteLine(x)

Real-World Use Cases

  1. Processing Collections
var numbers = new List<int> { 1, 2, 3, 4 }; 
// Use Func to transform numbers var squares = numbers.Select(x => x * x); 
Console.WriteLine(string.Join(", ", squares)); 
// Output: 1, 4, 9, 16 
// Use Action to print each number 
numbers.ForEach(x => Console.WriteLine($"Number: {x}"));
  1. Event Handling
Action<string> onMessageReceived = (msg) => Console.WriteLine($"Message: {msg}");
onMessageReceived("Hello from Action!"); 
// Output: Message: Hello from Action!
  1. Dynamic Calculation Logic
Func<int, int, double> divide = (x, y) => (double)x / y; 
Console.WriteLine(divide(10, 3)); 
// Output: 3.33333333333333

Key Points

  • Use Func for methods that return a value.
  • Use Action for methods that perform actions without returning a value.
  • Both are extensively used with LINQ, event handlers, and dynamic behaviors.

By davs