Func, Action and Predicate in C# With Example

By | August 15, 2015

Func, Action And Predicate Explained

I have covered the basics about the delegates in one of my previous blogs. In this article I would like to cover the three type of delegates viz. Func, Action and Predicate in C# with example, which are used very frequently as the arguments for the extension methods for collections provided by .NET framework.  But before start talking about these three type of delegates, I want to discuss about the Generics in delegates.

Become an Expert in Programming and Web Development.View all courses!

Before Going further you can learn.

  1. Delegates here
  2. Anonymous Function and Lambda Expression

Generic delegate type

A delgate type may contain generic type parameters as shown in the following code snippet.

A function which return the same type as the parameter type can be used as the function pointer for this delegate type.

As we can see from the above example MyDelegates instance has been used to call the two functions which return and accept the parameter, and return value of type string and int.
But if we want to achieve some thing in a different way, like suppose if we want the parameter of type int and return value of type string or both the input and return type are different in that case our delegate would be something as shown below.

The Func, Action and Predicate Delegates

As we can see from the above examples, with the help of generics we can write the delegate types that can take any type of parameters and return any type of results.

With this same capability .NET framwork has introduced a number of predefined delegates which can take any number of arguments and can also return the generic type of results. These are the Func and Action delegates, defined in the System namespace. These delegates has been introduced to remove the complexity of creating generic delegates as these delegates can take upto sixteen generic parameters.

  1. Action – This delegate is used as a function pointer for the method which can take upto 16 parameters and returns void . Some of the examples are as follows –

… the list goes upto T16

2.Func – This delegate is used as a function pointer for the method which can take upto 16 parameters and at least return                some type value . Some of the examples are as follows –

.. the list goes upto T16

3. Predicate – Predicate are the comparison delegates which takes only one generic argument and return bool. These delegates are generally used for the comparison related operations.

Now in the code example which I have described in starting of the article, in that example I can replace MyDelegate with Func delegate as shown below, with the functions call for the same functions

But this could not be the ideal scenario where Func and Action delegates are used. These delegate types are used more frequently for the Collection Extension methods as lambda expressions which I will discuss in my next article covering Anonymous methods and Lambda expressions.

I Hope you have liked this article about these delegate types. Kindly let me know your thoughts