Any easier way to write this if statement?
if (value==1 || value==2)
For example... in SQL you can say where value in (1,2)
instead of where value=1 or value=2
.
I'm looking for something that would work with any basic type... string, int, etc.
When you combine each one of them with an IF statement, they read like this: AND – =IF(AND(Something is True, Something else is True), Value if True, Value if False) OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False) NOT – =IF(NOT(Something is True), Value if True, Value if False)
The IF function allows you to make a logical comparison between a value and what you expect by testing for a condition and returning a result if True or False. So an IF statement can have two results. The first result is if your comparison is True, the second if your comparison is False.
Excel allows us to return multiple match result of a lookup using the TEXTJOIN and IF functions. This step by step tutorial will assist all levels of Excel users to learn how to return multiple match results in Excel.
How about:
if (new[] {1, 2}.Contains(value))
It's a hack though :)
Or if you don't mind creating your own extension method, you can create the following:
public static bool In<T>(this T obj, params T[] args) { return args.Contains(obj); }
And you can use it like this:
if (1.In(1, 2))
:)
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