C# Generic Delegates Func, Action, and Predicate

Initially I didn’t understand Func, Action and Predicate, but after doing some research I could understand and gather information. I am sharing this to all .NET readers.

You need to understand the basics of delegates before starting it.

What are Delegates?

Delegates are pointers to function. Delegates are used for implementing events and call back methods.

  1. using System;  
  2. usingSystem.Collections.Generic;  
  3. usingSystem.Linq;  
  4. usingSystem.Text;  
  5. usingSystem.Text.RegularExpressions;  
  6. usingSystem.Threading.Tasks;  
  7. namespace ConsoleApplication3  
  8. {  
  9.     public delegate intMyDelegate(intx, int y);  
  10.     public class DelegateClass  
  11.     {  
  12.         public static int Add(int x, int y)  
  13.         {  
  14.             returnx + y;  
  15.         }  
  16.         public static intGetValue(int x)  
  17.         {  
  18.             return(10 + x);  
  19.         }  
  20.         public static void ShowValue(int x)  
  21.         {  
  22.             Console.WriteLine("Value" + x);  
  23.         }  
  24.         public static intGetResult()  
  25.         {  
  26.             int x = 30;  
  27.             int y = 20;  
  28.             return x * y;  
  29.         }  
  30.         public static void ShowEmploye(intage, String name)  
  31.         {  
  32.             Console.WriteLine("My Name is" + name);  
  33.             Console.WriteLine("My age is " + age);  
  34.         }  
  35.         public static void ShowMessage(String msg)  
  36.         {  
  37.             Console.WriteLine(msg);  
  38.         }  
  39.         public static bool IsNumeric(object Expression)  
  40.         {  
  41.             doubleretNum;  
  42.             boolisNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);  
  43.             returnisNum;  
  44.         }  
  45.     }  
  46.     public class MainClass  
  47.     {  
  48.         public static void Main()  
  49.         {  
  50.             //Assign method to delegates  
  51.             MyDelegatedelAdd = DelegateClass.Add;  
  52.             //Call add method using custome delegates  
  53.             int sum = delAdd(1, 2);  
  54.             Console.WriteLine(sum);  
  55.             //You can't call other method which is having only one input parameter  
  56.             MyDelegate del = DelegateClass.GetValue; //Compilation error since signature of delegates not matching  
  57.         }  
  58.     }  
  59. }  
Func, Action and Predicate are define in C# 3.0 and these are generic inbuilt delegates. The above steps are not required, if you use these delegates.

Func Delegate

Func is generic delegate present in System namespace. It takes one or more input parameters and returns one out parameter. The last parameter is considered as a return value.
 
Func delegate type can include 0 to 16 input parameters of different types. It must have one return type. So return type is mandatory but input parameter is not.

Example1: Func delegate with two input parameters and one return value.
  1. Func func1 = DelegateClass.Add;  
  2. int value = func1(10, 20);  
  3. TParameter = 10,20;  
  4. TOutput = value = 30;  
Example 2: Func delegate with one input parameter and one return value.
  1. Func func2 = DelegateClass.GetValue;  
  2. int values = func2(30);  
  3. TParameter = 30  
  4. TOutput = 40;  
Example 3: Func delegate with zero input parameter and one return value.
  1. Func func3 = DelegateClass.GetResult;  
  2. intresultMulti = func3();  
  3. TParameter = Nothing  
  4. TOutput = 600;  
  5.   
  6. Func with Anonymous methods:  
  7. Func func4= delegate(intx,int y){ return (x+y); };  
  8. int result = func4(2,3);  
  9.   
  10. Func with Lambda expression:  
  11. Func func5= (intx,int y) => { return (x+y); };  
  12. int xx = func4(2,3);  
Action Delegate

Action is a generic delegate present in System namespace. It takes one or more input parameters and returns nothing.

So it does not return any value.

Example 1: Action delegate with two input parameters.
  1. Action action1=DelegateClass.ShowEmploye;  
  2. action1(30,"Rajesh");  
  3. TParameter = 30,Rajesh;  
  4. TOutput = Not available (No return value)  
Example 2: Action delegate with one input parameter.
  1. Action action2=DelegateClass.ShowMessage;  
  2. action2("Rajesh");  
  3. TParameter = “Rajesh”  
  4. TOutput = Not available  
Action delegate with Anonymous methods:
  1. Action action = delegate(String msg)  
  2. {  
  3.     Console.WriteLine(msg);  
  4. };  
  5. action("rajesh");  
  6. Action delegate with Lambda expression: Action action = (msg) = & gt;  
  7. {  
  8.     Console.WriteLine(msg)  
  9. };  
  10. action(“Rajesh”);  
Predicate Delegate

Predicate delegate is also inbuilt generic delegate and is present in System namespace.
It is used to verify certain criteria of method and returns output as Boolean, either True or False.

Predicate can be used with method, anonymous and lambda expression.

Example 1: Check String value is number using Predicate delegates.

Predicate predicate = DelegateClass.IsNumeric;
bool number = predicate("1234");

Example 2: Predicate delegate using Anonymous method.
  1. Predicate predicate = delegate(string str)  
  2. {  
  3.     double retNum;  
  4.     bool isNum = Double.TryParse(Convert.ToString(str), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);  
  5.     return isNum;  
  6. };  
  7. bool found = predicate("12232");  
Example 3: Predicate delegate using lambda expression.
  1. Predicate predicate = (str) = & gt;  
  2. {  
  3.     double retNum;  
  4.     bool isNum = Double.TryParse(Convert.ToString(str), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);  
  5.     return isNum;  
  6. };  
  7. bool found = predicate("12232");  
Summary

 

  • Func, Action and Predicate are generic inbuilt delegates present in System namespace.

  • All three can be used with method, anonymous method and lambda expression.

  • Func can contains 0 to 16 input parameters and must have one return type.

  • Action can contain 1 to 16 input parameters and does not have any return type.

  • Predicate delegate should satisfy some criteria of method and must have one input parameter and one Boolean return type either true or false.

  • Input parameters of custom delegates is fixed but Func and Actions input parameter is variable from 0 to 16 and 1 to 16 respectively.