The Func, Action and C# with Example



The Func, Action and C# with Example

Predicate delegate types 
The System namespace contains Func<..., TResult> delegate types with between 0 and 15 generic parameters, 
returning type TResult. 
private void UseFunc(Func func) 
{ 
string output = func(); // Func with a single generic type parameter returns that type 
Console.WriteLine(output); 
} 
private void UseFunc(Func func) 
{ 
string output = func(4, 2); // Func with multiple generic type parameters takes all but the 
first as parameters of that type 
Console.WriteLine(output); 
} 
The System namespace also contains Action<...> delegate types with different number of generic parameters 
(from 0 to 16). It is similar to Func, but it always returns void. 
private void UseAction(Action action) 
{ 
action(); // The non-generic Action has no parameters 
} 
private void UseAction(Action action) 
{ 
action(4, "two"); // The generic action is invoked with parameters matching its type arguments 
} 
Predicate is also a form of Func but it will always return bool. A predicate is a way of specifying a custom 
criteria. Depending on the value of the input and the logic defined within the predicate, it will return either true or 
false. Predicate therefore behaves in the same way as Func and both can be initialized and used in 
the same way. 
Predicate predicate = s => s.StartsWith("a"); 
Func func = s => s.StartsWith("a"); 
// Both of these return true 
var predicateReturnsTrue = predicate("abc"); 
var funcReturnsTrue = func("abc"); 
 

// Both of these return false 
var predicateReturnsFalse = predicate("xyz"); 
var funcReturnsFalse = func("xyz"); 
The choice of whether to use Predicate or Func is really a matter of opinion. Predicate is 
arguably more expressive of the author's intent, while Func is likely to be familiar to a greater proportion 
of C# developers. 
In addition to that, there are some cases where only one of the options is available, especially when interacting with 
another API. For example List and Array generally take Predicate for their methods, while most LINQ 
extensions only accept Func. 

0 Comment's

Comment Form

Submit Comment