Click here to Skip to main content
14,659,852 members
Articles » General Reading » Uncategorised Technical Blogs » General
Technical Blog
Posted 15 Aug 2015

Tagged as

Stats

41.9K views
5 bookmarked

Func, Action and Predicate in C# With Example

Rate this:
2.50 (2 votes)
Please Sign up or sign in to vote.
2.50 (2 votes)
15 Aug 2015CPOL
Func, Action and Predicate in C# with an example

Func, Action And Predicate Explained

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

Generic Delegate Type

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

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

A function which returns 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 something 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 Framework 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 have 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 – Predicates are the comparison delegates which take 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 at the beginning of the article, in that example, I can replace MyDelegate with Func delegate as shown below, with the function 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 liked this article about these delegate types. Kindly let me know your thoughts.

The post Func, Action and Predicate in C# With Example appeared first on Dot Net For All.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

DotNetForAll
Software Developer (Senior)
India India
I have 8 years of experience of mostly developing the .NET windows as well as web applications. Passionate about learning and sharing.

Visit my personal blog to know more about the .NET technology and increase your knowledge about C# and .NET. You can always follow me on twitter @dotnetforall.

Comments and Discussions

 
QuestionWhat are the "in" here? Pin
Member 105358112-Aug-17 21:30
MemberMember 105358112-Aug-17 21:30 
AnswerRe: What are the "in" here? Pin
DotNetForAll25-Feb-18 19:03
MemberDotNetForAll25-Feb-18 19:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.