Dot Net For All

Func, Action and Predicate in C# With Example

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.

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.

public delegate T MyDelegate<T>(T arg);

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

        public delegate T MyDelegate<T>(T arg);

        static void Main(string[] args)
        {
            MyDelegate<string> strDelegate = new MyDelegate<string>(LetStringDoTheWork);
            Console.Write(strDelegate("Programer"));

            MyDelegate<int> intDelegate = new MyDelegate<int>(LetIntDoTheWork);
            Console.Write("\n" + intDelegate(12));

            Console.ReadKey();
        }

        public static string LetStringDoTheWork(string strPara)
        {
            return strPara.ToString();
        }

        public static int LetIntDoTheWork(int intPara)
        {
            return intPara;
        }

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.

public delegate TRes MyDelegate<T, TRes>(T arg);

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 –
        delegate void Action();
        delegate void Action<in T>(T arg);        
        delegate void Action<in T1, in T2>(T1 arg, T2 arg);

… 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 –

        delegate TResult Func<out TResult>();
        delegate TResult Func<in T,out TResult>(T arg);
        delegate TResult Func<in T1, in T2,out TResult>(T1 arg, T2 arg);

.. 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.

public delegate bool Predicate<in T>(T obj);

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

            Func<string, string> func = LetStringDoTheWork;
            Console.Write(func("string"));

            Func<int, int> funcInt = LetIntDoTheWork;
            Console.Write(funcInt(12));

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

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview