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.
- using System;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingSystem.Text.RegularExpressions;
- usingSystem.Threading.Tasks;
- namespace ConsoleApplication3
- {
- public delegate intMyDelegate(intx, int y);
- public class DelegateClass
- {
- public static int Add(int x, int y)
- {
- returnx + y;
- }
- public static intGetValue(int x)
- {
- return(10 + x);
- }
- public static void ShowValue(int x)
- {
- Console.WriteLine("Value" + x);
- }
- public static intGetResult()
- {
- int x = 30;
- int y = 20;
- return x * y;
- }
- public static void ShowEmploye(intage, String name)
- {
- Console.WriteLine("My Name is" + name);
- Console.WriteLine("My age is " + age);
- }
- public static void ShowMessage(String msg)
- {
- Console.WriteLine(msg);
- }
- public static bool IsNumeric(object Expression)
- {
- doubleretNum;
- boolisNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
- returnisNum;
- }
- }
- public class MainClass
- {
- public static void Main()
- {
-
- MyDelegatedelAdd = DelegateClass.Add;
-
- int sum = delAdd(1, 2);
- Console.WriteLine(sum);
-
- MyDelegate del = DelegateClass.GetValue;
- }
- }
- }
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.
- Func func1 = DelegateClass.Add;
- int value = func1(10, 20);
- TParameter = 10,20;
- TOutput = value = 30;
Example 2: Func delegate with one input parameter and one return value.
- Func func2 = DelegateClass.GetValue;
- int values = func2(30);
- TParameter = 30
- TOutput = 40;
Example 3: Func delegate with zero input parameter and one return value.
- Func func3 = DelegateClass.GetResult;
- intresultMulti = func3();
- TParameter = Nothing
- TOutput = 600;
-
- Func with Anonymous methods:
- Func func4= delegate(intx,int y){ return (x+y); };
- int result = func4(2,3);
-
- Func with Lambda expression:
- Func func5= (intx,int y) => { return (x+y); };
- 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.
- Action action1=DelegateClass.ShowEmploye;
- action1(30,"Rajesh");
- TParameter = 30,Rajesh;
- TOutput = Not available (No return value)
Example 2: Action delegate with one input parameter.
- Action action2=DelegateClass.ShowMessage;
- action2("Rajesh");
- TParameter = “Rajesh”
- TOutput = Not available
Action delegate with Anonymous methods:
- Action action = delegate(String msg)
- {
- Console.WriteLine(msg);
- };
- action("rajesh");
- Action delegate with Lambda expression: Action action = (msg) = & gt;
- {
- Console.WriteLine(msg)
- };
- 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.
- Predicate predicate = delegate(string str)
- {
- double retNum;
- bool isNum = Double.TryParse(Convert.ToString(str), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
- return isNum;
- };
- bool found = predicate("12232");
Example 3: Predicate delegate using lambda expression.
- Predicate predicate = (str) = & gt;
- {
- double retNum;
- bool isNum = Double.TryParse(Convert.ToString(str), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
- return isNum;
- };
- 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.