Instead of
if (foo == "1" || foo == "5" || foo == "9" ... )
I like to combine them similar to the following (which doesn't work):
if (foo == ("1" || "5" || "9" ... ))
Is that possible?
The logical operators && ("and") and || ("or") combine conditional expressions; the ! ("not") operator negates them. The ! has the highest precedence, then && , then || ; you will need parentheses to force a different order.
Use two if statements if both if statement conditions could be true at the same time. In this example, both conditions can be true. You can pass and do great at the same time. Use an if/else statement if the two conditions are mutually exclusive meaning if one condition is true the other condition must be false.
Technically only 1 condition is evaluated inside an if , what is ascually happening is 1 condition gets evaluated and its result is evaluated with the next and so on...
Unfortunately not, your best bet is to create an extension method
public static bool IsOneOf<T>(this T value, params T[] options) { return options.Contains(value); }
and you can use it like this:
if (foo.IsOneOf("1", "5", "9")) { ... }
Being generic, it can be used for any type (int, string etc).
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