Home
 
  

Func, Action and Predicate

March 15, 2014

Func, Action and Predicate are predefined generic delegates which are included with .NET framework. Func refer method which always return a value, Action always refer a method which dont return value and predicate which always take one argument and return always bool type value.

let understand their signature, I have decompiled the mscorlib and navigate to System namespace and found these signature for

  1. Func : Func have nine kinds of signature in param are input type and out param is return type, maximum 8 input parameters
  1. Action : Action have eight kind of signature, maximum 8 input parameters
  1. Predicate : Predicate have only one input parameter and returns always boo type.

let understand with code example, I used Math delegate in my previous blog on delegate, Anonymous methods and Lambda notation that Math delegate is equivalent of

Func<int,int,int>
// delegate int Math(int value1, int value2);

now I can use this Func delegate to refer Add method , in this example I just refer Add method to math delegate. I am using Func with 2 int input param with int return type.

class Program
{
static void Main(string[] args)
{
Func<int, int, int> math = Add;
Console.Write(math(1, 1));
}
public static int Add(int a, int b)
{
return a + b;
}
}
//output
//2

we can also assign anonymous method, in this I have created a anonymous method through delegate

class Program
{
static void Main(string[] args)
{
Func<int, int, int> math = delegate(int a, int b)
{
return a + b;
};
Console.Write(math(1, 1));
}
}
//output
//2

we can also assign anonymous method through lambda notation

class Program
{
static void Main(string[] args)
{
Func<int, int, int> math = (a, b) => { return a + b; };
Console.Write(math(1, 1));
}
}
//output
//2

or if anonymous method have single statement then

class Program
{
static void Main(string[] args)
{
Func<int, int, int> math = (a, b) => a + b;
Console.Write(math(1, 1));
}
}
//output
//2

Action never return value, following example I have created a delegate which takes one int input parameter and Print on console.

class Program
{
static void Main(string[] args)
{
Action<int> Print = (a) => { Console.Write(a); };
Print(10);
}
}
//output
//10

You can pass multiple input parameters up to 8 , in following example i am passing 3 input parameter 2 string type and 1 int type.

class Program
{
static void Main(string[] args)
{
Action<string,string,int> PrintResult = (name,regNo,marks) => { Console.Write(string.Format("Name : {0} nRegisteration No. : {1} nMarks : {2} ",name,regNo,marks)); };
PrintResult("Mark", "RJ24", 390);
}
}
//output
//Name : Mark
//Registeration No. : RJ24
//Marks : 390

And now finally predicate takes only one input parameter and return always bool type

class Program
{
static void Main(string[] args)
{
Predicate<int> IsLessThenTen = (a) => { return a < 10; };
Console.WriteLine(IsLessThenTen(10));
Console.WriteLine(IsLessThenTen(2));
}
}
//output
//False
//True

its equivalent to Func<int,bool> , check following example

class Program
{
static void Main(string[] args)
{
Func<int,bool> IsLessThenTen = (a) => { return a < 10; };
Console.WriteLine(IsLessThenTen(10));
Console.WriteLine(IsLessThenTen(2));
}
}
//output
//False
//True

You can pass Func, Action and Predicate as parameter also, check following example

class Program
{
static void Main(string[] args)
{
var namesstartwithm = GetNames(name => name.StartsWith("M"));
Console.WriteLine("--- Name which are starts with M char ------");
foreach (var name in namesstartwithm)
{
Console.WriteLine(name);
}
var names = GetNames(name => name.Count()<=3);
Console.WriteLine("nn--- Name which are contains less then or equals to 3 chars ------");
foreach (var name in names)
{
Console.WriteLine(name);
}
Console.ReadLine();
}
public static IEnumerable<string> GetNames(Func<String,bool> namecriteria)
{
List<string> names = new List<string>();
names.Add("John");
names.Add("Martin");
names.Add("Tom");
names.Add("Nick");
names.Add("Geoge");
names.Add("Matt");
foreach (var name in names.AsEnumerable())
{
if (namecriteria(name))
{
yield return name;
}
}
}
}
//output
//
//--- Name which are starts with M char ------
//Martin
//Matt
//--- Name which are contains less then or equals to 3 chars ------
//Tom
Govind Malviya

Govind Malviya

Director of Product Development @LoginRadius

 

© 2020