In C# .NET, delegates are a powerful feature that allows you to pass methods as arguments to other methods. Among the most commonly used delegates are Func<T>, Action<T>, and Predicate<T>. These predefined delegates simplify the use of delegates in your applications and make your code more readable and maintainable. This article will delve into each of these delegates, exploring their definitions, use cases, and examples.
Func<T>
The Func delegate represents a method that returns a value. It can take up to 16 input parameters and must return a value. The last type parameter in the Func delegate is always the return type, while the preceding parameters are input types.
Syntax
public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2);
Examples
-
Func with One Parameter
Func<int, int> square = x => x * x;
int result = square(5); // result = 25
-
Func with Multiple Parameters
Func<int, int, int> add = (x, y) => x + y;
int sum = add(3, 4); // sum = 7
-
Func with No Parameters
Func<DateTime> getCurrentTime = () => DateTime.Now;
DateTime now = getCurrentTime(); // now = current date and time
Use Cases
- Performing computations or transformations.
- Simplifying method signatures in LINQ queries.
- Creating higher-order functions.
Action<T>
The Action delegate represents a method that performs an action but does not return a value. It can take up to 16 input parameters but always returns void.
Syntax
public delegate void Action<in T1, in T2>(T1 arg1, T2 arg2);
Examples
-
Action with One Parameter
Action<string> print = message => Console.WriteLine(message);
print("Hello, World!"); // Output: Hello, World!
-
Action with Multiple Parameters
Action<int, int> printSum = (x, y) => Console.WriteLine(x + y);
printSum(5, 10); // Output: 15
-
Action with No Parameters
Action displayTime = () => Console.WriteLine(DateTime.Now);
displayTime(); // Output: current date and time
Use Cases
- Executing operations without needing a return value.
- Logging or printing messages.
- Event handling.
Predicate<T>
The Predicate delegate represents a method that takes one input parameter and returns a boolean value. It is often used for filtering or condition checking.
Syntax
public delegate bool Predicate<in T>(T obj);
Examples
-
Predicate with One Parameter
Predicate<int> isEven = x => x % 2 == 0;
bool result = isEven(4); // result = true
-
Using Predicate in a List
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };
List<int> evenNumbers = numbers.FindAll(isEven); // evenNumbers = [2, 4, 6]
Use Cases
- Filtering collections.
- Condition checks in algorithms.
- Validation logic.
Conclusion
The Func, Action, and Predicate delegates in C# .NET are essential tools for working with methods as first-class objects. They provide a flexible and concise way to pass methods around, enhancing the expressiveness and readability of your code. Understanding these delegates will help you write more functional and elegant C# code.
By mastering Func, Action, and Predicate, you can leverage the full power of delegates in your .NET applications, making your codebase more versatile and maintainable.