Is there any way to do something similar in C#?
ie.
i++ unless i > 5;
here is another example
weatherText = "Weather is good!" unless isWeatherBad
                You can achieve something like this with extension methods. For example:
public static class RubyExt
{
    public static void Unless(this Action action, bool condition)
    {
        if (!condition)
            action.Invoke();
    }
}
and then use it like
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);
                        What about :
if (i<=5) i++;
if (!(i>5)) i++; would work too.
Hint : There is no unless exact equivalent.
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