The purpose of writing this article is simple; to provide a simple and fresh demonstration of Action vs Func vs Predicate keyword in C#.
Though widely used with Linq, Action and Func are concepts logically independent of Linq. C++ already contained the basic concept in form of typed function pointers.
- Action is a delegate (pointer) to a method, that takes zero, one or more input parameters, but does not return anything.
- Func is a delegate (pointer) to a method, that takes zero, one or more input parameters, and returns a value (or reference).
- Predicate is a function pointer for method which returns boolean value. They are commonly used for iterating a collection or to verify if the value does already exist.
Here is a small example for Action, Func and Predicate without using Linq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
class Program {
static void Main(string[] args) {
Action myAction = new Action(DoSomething);
myAction.Invoke(123); // Prints out "123"
Func < int, double > myFunc = new Func < int, double > (CalculateSomething);
Console.WriteLine(myFunc(5)); // Prints out "2.5"
Predicate tempPredicatePointer = FourthTestFunction;
Employee[] lstEmployee = (new Employee[] {
new Employee() {
Name = "Ashwin", Age = 31
},
new Employee() {
Name = "Akil", Age = 25
},
new Employee() {
Name = "Amit", Age = 28
},
new Employee() {
Name = "Ajay", Age = 29
},
});
Employee tempEmployee = Array.Find(lstEmployee, tempPredicatePointer);
Console.WriteLine("Person below 27 age :" + tempEmployee.Name);
Console.ReadKey();
}
static void DoSomething(int i) {
Console.WriteLine(i);
}
static double CalculateSomething(int i) {
return (double) i / 2;
}
static bool FourthTestFunction(Employee employee) {
return employee.Age < 27;
}
}
|
Detailed examples. We take a closer look at lambdas and anonymous functions. The => operator separates the parameters to a method from its statements in the method's body.
Tip:Lambda expressions use the token => in an expression context. In this context, the token is not a comparison operator.
Goes to:The => operator can be read as "goes to." It is always used when declaring a lambda expression.
Invoke:With Invoke, a method on Func and Action, we execute the methods in the lambdas.
C# program that uses lambda expressions using System; class Program { static void Main() { // // Use implicitly-typed lambda expression. // ... Assign it to a Func instance. // Func<int, int> func1 = x => x + 1; // // Use lambda expression with statement body. // Func<int, int> func2 = x => { return x + 1; }; // // Use formal parameters with expression body. // Func<int, int> func3 = (int x) => x + 1; // // Use parameters with a statement body. // Func<int, int> func4 = (int x) => { return x + 1; }; // // Use multiple parameters. // Func<int, int, int> func5 = (x, y) => x * y; // // Use no parameters in a lambda expression. // Action func6 = () => Console.WriteLine(); // // Use delegate method expression. // Func<int, int> func7 = delegate(int x) { return x + 1; }; // // Use delegate expression with no parameter list. // Func<int> func8 = delegate { return 1 + 1; }; // // Invoke each of the lambda expressions and delegates we created. // ... The methods above are executed. // Console.WriteLine(func1.Invoke(1)); Console.WriteLine(func2.Invoke(1)); Console.WriteLine(func3.Invoke(1)); Console.WriteLine(func4.Invoke(1)); Console.WriteLine(func5.Invoke(2, 2)); func6.Invoke(); Console.WriteLine(func7.Invoke(1)); Console.WriteLine(func8.Invoke()); } } Output 2 2 2 2 4 2 2
No comments:
Post a Comment