Passing a condition as a parameter

Is it possible to pass a condition as a parameter, as with actions?

Here is an example.

public void Test(Action action, Condition condition); 

...

 Test( () => Environment.Exit(0), () => variable == variable2 ); 
+4
source share
1 answer

Try passing the second argument as type Func<Boolean> . The code should work as it is in the second part of your question:

 public void Text(Action action, Func<Boolean> condition) { if (condition()) action(); } 

EDIT: Note that what you would do in your use case creates a Closure containing the captured variable variable and variable2. You must understand the consequences of closure before using them this way.

+13
source

All Articles