In C#, Action, Predicate, and Func are powerful delegates that help us write flexible and reusable code. Letβs break down what they do and when to use them.
Action
- Represents a method that returns void.
- Can take 0 to 16 input parameters.
- Commonly used for operations that don't return a value.
Predicate
- Represents a method that returns a boolean (bool).
- Takes exactly one input parameter of type T.
- Ideal for conditions and filtering.
Func
- Represents a method that returns a value.
- Can take 0 to 16 input parameters.
- The last type parameter defines the return type.
Conclusion
- Understanding Action, Predicate, and Func helps you:
- Write more concise and expressive code.
- Avoid unnecessary custom delegates.
- Work easily with LINQ, events, and functional programming patterns.
- Use them wisely to make your C# code cleaner, modular, and reusable
Top comments (1)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.