Kon*_*nos 13 c# ruby if-statement
有没有办法在C#中做类似的事情?
即.
i++ unless i > 5;
Run Code Online (Sandbox Code Playgroud)
这是另一个例子
weatherText = "Weather is good!" unless isWeatherBad
Run Code Online (Sandbox Code Playgroud)
avi*_*ivr 21
你可以通过扩展方法实现这样的目标.例如:
public static class RubyExt
{
public static void Unless(this Action action, bool condition)
{
if (!condition)
action.Invoke();
}
}
Run Code Online (Sandbox Code Playgroud)
然后像使用它一样
int i = 4;
new Action(() => i++).Unless(i < 5);
Console.WriteLine(i); // will produce 4
new Action(() => i++).Unless(i < 1);
Console.WriteLine(i); // will produce 5
var isWeatherBad = false;
var weatherText = "Weather is nice";
new Action(() => weatherText = "Weather is good!").Unless(isWeatherBad);
Console.WriteLine(weatherText);
Run Code Online (Sandbox Code Playgroud)
Dr.*_*eon 11
关于什么 :
if (i<=5) i++;
Run Code Online (Sandbox Code Playgroud)
if (!(i>5)) i++;
也会工作.
提示:没有unless
确切的等价物.
归档时间: |
|
查看次数: |
3997 次 |
最近记录: |