How to define a predicate as a function argument
.net, c#, predicate
Solution
Here's a trivial example of using a predicate in a function.
static void CheckRandomValueAgainstCriteria(Predicate<int> predicate, int maxValue)
{
Random random = new Random();
int value = random.Next(0, maxValue);
Console.WriteLine(value);
if (predicate(value))
{
Console.WriteLine("The random value met your criteria.");
}
else
{
Console.WriteLine("The random value did not meet your criteria.");
}
}
...
CheckRandomValueAgainstCriteria(i => i < 20, 40);
Problem
I want to be able to write something as ``` void Start(some condition that might evaluate to either true or false) { //function will only really start if the predicate evaluates to true } ``` I'd guess it must be something of the form: ``` void Start(Predicate predicate) { } ``` How can I check inside my Start function whenever the predicate evaluated to true or false? Is my use of a predicate correct? Thanks