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
- Basic Usage
Func<int, int, int> add = (x, y) => x + y;
Console.WriteLine(add(5, 3));
// Output: 8- Single Input, Single Output
Func<int, string> convertToString = (number) => $"Number: {number}";
Console.WriteLine(convertToString(42));
// Output: Number: 42- No Parameters
Func<string> getGreeting = () => "Hello, Func!";
Console.WriteLine(getGreeting());
// Output: Hello, Func!- Using
Funcas 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: 52. 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
- Basic Usage
Action<string> printMessage = (message) => Console.WriteLine(message);
printMessage("Hello, Action!");
// Output: Hello, Action!- Multiple Parameters
Action<int, int> printSum = (x, y) => Console.WriteLine($"Sum: {x + y}");
printSum(3, 4);
// Output: Sum: 7- No Parameters
Action printHello = () => Console.WriteLine("Hello, World!");
printHello();
// Output: Hello, World!- Using
Actionas a Parameter
public static void ProcessAction(Action<string> action, string value)
{
action(value);
}
ProcessAction((msg) => Console.WriteLine($"Processed: {msg}"), "Action Test");
// Output: Processed: Action TestComparison of Func and Action
| Feature | Func | Action |
|---|---|---|
| Purpose | For methods that return a value | For methods with no return value |
| Return Type | Has a return type (TResult) | Always void |
| Parameters | Can have zero or more input parameters | Can have zero or more input parameters |
| Example | Func<int, int, int> (x, y) => x + y | Action<int> (x) => Console.WriteLine(x) |
Real-World Use Cases
- 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}"));- Event Handling
Action<string> onMessageReceived = (msg) => Console.WriteLine($"Message: {msg}");
onMessageReceived("Hello from Action!");
// Output: Message: Hello from Action!- Dynamic Calculation Logic
Func<int, int, double> divide = (x, y) => (double)x / y;
Console.WriteLine(divide(10, 3));
// Output: 3.33333333333333Key Points
- Use
Funcfor methods that return a value. - Use
Actionfor methods that perform actions without returning a value. - Both are extensively used with LINQ, event handlers, and dynamic behaviors.
