开发者

Passing a condition as a parameter

开发者 https://www.devze.com 2023-03-30 09:02 出处:网络
Is it possible to pass a condition as parameter as you do with actions? Here\'s an example. public void Test(Action action, Condition condition);

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

Here's an example.

public void Test(Action action, Condition condition);

...

Test( () => Environment.Exit(0), () => variable == varia开发者_Go百科ble2 );


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

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

EDIT: Note that what you would be doing in your usage example is creating a Closure containing the captured variables variable and variable2. You should understand the implications of closures before using them in this way.

0

精彩评论

暂无评论...
验证码 换一张
取 消