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

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

Advanced delegates

Advanced delegates

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.

A Predicate is a delegate that accepts one or more generic parameters and returns a Boolean value -- you can assume it something like Func<T,bool>. Predicate delegates are typically used to perform search operations on some data based on a set of criteria.

Programming Action delegates in C#

You can take advantage of delegates in C# to implement events and call back methods. A delegate in C# is similar to function pointers of C++, but C# delegates are type safe. You can pass methods as parameters to a delegate to allow the delegate to point to the method.

The following code snippet illustrates the syntax for using Action delegate.

Action<TParameter>

The following code listing shows how you can use Action delegate. This code snippet when executed would print the word "Hello!!!" in the console window.

static void Main(string[] args)

        {

            Action<string> action = new Action<string>(Display);

            action("Hello!!!");

            Console.Read();

        }

static void Display(string message)

        {

            Console.WriteLine(message);

        }

Programming Func delegates in C#

Let's now understand how we can work with Func delegates in C#. Here's the syntax for a Func delegate.

Func<TParameter, TOutput>

The following code snippet illustrates how you can use a Func delegate in C#. It prints the value of Hra (calculated as 40% of basic salary). The basic salary is passed to it as an argument.

static void Main(string[] args)

        {

            Func<int, double> func = new Func<int, double>(CalculateHra);

            Console.WriteLine(func(50000));

            Console.Read();

        }

        static double CalculateHra(int basic)

        {

            return (double)(basic * .4);

        }

Note that the second parameter in the declaration of the Func delegate in the code snippet given earlier represents the return type of the method to which the delegate would point -- in this example, the calculated Hra value is returned as double.

Programming Predicate delegates in C#

A Predicate delegate is typically used to search items in a collection or a set of data. Here's how the syntax of Predicate delegate looks like.

Predicate<T>

Note that Predicate<T> is basically equivalent to Func<T,bool>.

Consider the following entity class named Customer.

class Customer

    {

        public int Id { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string Address { get; set; }

        public string City { get; set; }

        public string State { get; set; }

        public string Country { get; set; }

    }

Next, create a list of customers and store objects of type Customer into it.

 List<Customer> custList = new List<Customer>();

            custList.Add(new Customer { Id = 1, FirstName = "Joydip", LastName = "Kanjilal", State = "Telengana", City = "Hyderabad", Address = "Begumpet", Country = "India" });

            custList.Add(new Customer { Id = 2, FirstName = "Steve", LastName = "Jones", State = "OA", City = "New York", Address = "Lake Avenue", Country = "US" });

The following is the complete code listing that shows how we can use Predicate delegate to search data.

static void Main(string[] args)

        {

            List<Customer> custList = new List<Customer>();

            custList.Add(new Customer { Id = 1, FirstName = "Joydip", LastName = "Kanjilal", State = "Telengana", City = "Hyderabad", Address = "Begumpet", Country = "India" });

            custList.Add(new Customer { Id = 2, FirstName = "Steve", LastName = "Jones", State = "OA", City = "New York", Address = "Lake Avenue", Country = "US" });

            Predicate<Customer> hydCustomers = x => x.Id == 1;

            Customer customer = custList.Find(hydCustomers);

            Console.WriteLine(customer.FirstName);

            Console.Read();

        }

When the above code snippet is executed, the name "Joydip" will be displayed in the console window.

This article is published as part of the IDG Contributor Network. Want to Join?

From CIO: 8 Free Online Courses to Grow Your Tech Skills
Notice to our Readers
We're now using social media to take your comments and feedback. Learn more about this here.