How to work with Action, Func, and Predicate delegates in C#

Learn how you can take advantage of delegates like Action, Func, and Predicate to facilitate callbacks and add flexibility to your code.

How to work with Action, Func, and Predicate delegates in C#
Thinkstock

A delegate is a type-safe function pointer that can reference a method that has the same signature as that of the delegate. Delegates are used to define callback methods and implement event handling, and they are declared using the “delegate” keyword. You can declare a delegate that can appear on its own or even nested inside a class.

What are Func and Action delegates? How can they be used?

The basic difference between Func and Action delegates is that while the former is used for delegates that return value, the latter can be used for those delegates in which you don't have any return value.

Func is a delegate that points to a method that accepts one or more arguments and returns a value. Action is a delegate that points to a method which in turn accepts one or more arguments but returns no value. In other words, you should use Action when your delegate points to a method that returns void.

Copyright © 2016 IDG Communications, Inc.

How to choose a low-code development platform