1.

What is Func, Action and Predicate?

Answer»

Func, Action and PREDICATE are define in C# 3.0 and these are generic inbuilt 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");


Discussion

No Comment Found