C Sharp Generic delegates Func, Action and Predicate with anonymous method

Understand the built-in generic delegates Func, Action, and Predicate in C#, and how they work with anonymous methods.

The Func Delegate

Func is a generic delegate that represents a method returning a value, with the last type parameter specifying the return type and any preceding ones specifying input parameters.

The Action Delegate

Action is a generic delegate used for methods that perform an operation but do not return a value, making it ideal for simple callback-style logic.

The Predicate Delegate

Predicate represents a method that takes one input and returns a boolean, commonly used for filtering or testing conditions on collection elements.

Using Anonymous Methods

Anonymous methods, defined using the delegate keyword without a separate named method, let you write inline logic directly where a delegate is expected, which is especially handy with Func, Action, and Predicate.

Conclusion: Func, Action, and Predicate cover the vast majority of delegate scenarios in C#, and combining them with anonymous methods or lambda expressions leads to concise, expressive code.

Ready to Master C# Programming Course?

Join Uncodemy's hands-on training program with expert mentors and real-world projects.