Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to shorten an OR statement with multiple conditions on the same variable?

Tags:

c#

Is there a way of shortening if(a=="bcd" || a=="efg")?

Because the conditions are both on a It would be nice to write something like if(a=="bcd"||"efg") and contract the statement. I know I could write something custom to handle it, but was wondering if there is anything built into C# that would accomplish it?

like image 977
Dan Avatar asked Jun 02 '21 08:06

Dan


People also ask

How do you write an if statement with multiple conditions?

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)

Can you have multiple conditions in an if statement Java?

Java else if StatementWe can use multiple if and else statements as long as a condition is not met. Note: we can have multiple else if statements but we always terminate with an else statement.


1 Answers

With C# 9's patterns, you can write:

if (a is "bcd" or "efg")
{

}
like image 139
canton7 Avatar answered Oct 21 '22 11:10

canton7