In C#, both Action and Function (Func<>) are delegates, but they serve different purposes.
πΉ Action β‘ Represents a method that does not return a value (void).
Action printMessage = message => Console.WriteLine(message);
printMessage("Hello, LinkedIn!");
Here, printMessage takes a string as input and prints it but doesnβt return anything.
πΉ Func β‘ Represents a method that returns a value.
Func add = (a, b) => a + b;
int sum = add(5, 10);
Console.WriteLine(sum); // Output: 15
Here, add takes two integers and returns their sum.
Key Takeaway: Use Action when you donβt need a return value and Func when you do!
Top comments (0)