Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the difference between Func delegate and Action delegate in C#?
A delegate is a type that represents references to methods with a particular parameter list and return type. When we instantiate a delegate, we can associate its instance with any method with a compatible signature and return type. We can invoke (or call) the method through the delegate instance.
The .NET Framework provides two built-in generic delegate types: Func and Action. Both serve different purposes based on whether the method returns a value or not.
Func Delegate
The Func delegate is a generic delegate included in the System namespace. It has zero or more input parameters and one out parameter. The last parameter is always considered the return type. This delegate can point to a method that takes up to 16 parameters and returns a value.
Syntax
Following is the syntax for declaring a Func delegate −
Func<TResult> // No parameters, returns TResult Func<T, TResult> // One parameter, returns TResult Func<T1, T2, TResult> // Two parameters, returns TResult
Example
using System;
namespace DemoApplication {
class Program {
static void Main(string[] args) {
Func<string, string, string> func = Append;
string fullName = func("Michael", "Jackson");
Console.WriteLine(fullName);
// Using lambda expression
Func<int, int, int> add = (x, y) => x + y;
Console.WriteLine("Sum: " + add(5, 3));
}
static string Append(string firstName, string lastName) {
return firstName + " " + lastName;
}
}
}
The output of the above code is −
Michael Jackson Sum: 8
Action Delegate
The Action delegate is a delegate type defined in the System namespace. An Action delegate is the same as Func delegate except that the Action delegate doesn't return a value. In other words, an Action delegate can be used with a method that has a void return type. It can contain a minimum of 0 and maximum of 16 input parameters.
Syntax
Following is the syntax for declaring an Action delegate −
Action // No parameters, void return Action<T> // One parameter, void return Action<T1, T2> // Two parameters, void return
Example
using System;
namespace DemoApplication {
class Program {
static void Main(string[] args) {
Action<string, string> func = AppendPrint;
func("Michael", "Jackson");
// Using lambda expression
Action<string> greet = name => Console.WriteLine("Hello, " + name);
greet("Alice");
}
static void AppendPrint(string firstName, string lastName) {
string fullName = firstName + " " + lastName;
Console.WriteLine($"{fullName}");
}
}
}
The output of the above code is −
Michael Jackson Hello, Alice
Comparison
| Aspect | Func Delegate | Action Delegate |
|---|---|---|
| Return Type | Must return a value | No return value (void) |
| Parameters | 0-16 input parameters + 1 return type | 0-16 input parameters |
| Use Case | Methods that compute and return results | Methods that perform actions without returning values |
| Example |
Func<int, int, int> for addition |
Action<string> for printing |
Using Both Delegates Together
Example
using System;
namespace DemoApplication {
class Program {
static void Main(string[] args) {
// Func to calculate square
Func<int, int> square = x => x * x;
// Action to print result
Action<int> printResult = result => Console.WriteLine("Result: " + result);
int number = 5;
int result = square(number);
printResult(result);
// Chain them together
printResult(square(8));
}
}
}
The output of the above code is −
Result: 25 Result: 64
Conclusion
The key difference between Func and Action delegates is that Func returns a value while Action does not. Use Func for methods that compute and return results, and Action for methods that perform operations without returning values. Both delegates support lambda expressions and can greatly simplify callback scenarios in C# programming.
