I am trying to pass an expression as parameter to a function like:
public string MyFunction(bool expression) //what should I set here as parameter type?
{
return expression ? "True" : "False";
}
public void InvokeMyFunction()
{
var myString = MyFunction(10 > 1);
}
You can pass Func<bool> to represent a method that returns a boolean. Also when you declare the function, you can use lambda syntax () => to represent an anonymous method.
string MyFunction(Func<bool> expression)
{
return expression() ? "True" : "False";
}
void InvokeMyFunction()
{
var myString = MyFunction(() => 10 > 1);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With