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 (1)
Short and to the point. I like. Fairly new to C#. Question: what's the advantage of using
vs