DEV Community

Anoop
Anoop

Posted on

Action vs. Function in C# – What's the Difference?

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)