-1

I'm looking for short coding style for the if condition containing multiple expression.

Let's suppose, I want to add if condition in a string variable name str. Here is my if condition.

if(str == "a" || str == "b" || str == "c" || str == "d")

I want to make this if condition short something like that.

if(str == "a" || "b" || "c" || "d")

or

if (str in {"a", "b", "c", "d"})

I don't have to put str == again and again.

I know, I can do this using multiple ways:

  • Using switch statement
  • Making array/list of string and use LINQ or Contains to check.

I want to do some something similar to if(str == "a" || "b" || "c" || "d") or if (str in {"a", "b", "c", "d"})

Thank you for your help!

Saadi
  • 2,211
  • 4
  • 21
  • 50

2 Answers2

1

There is NO such syntax available. This is the simple answer to your requirement.

OR

You can follow what Dmitry Bychenko suggested in comments.

if (new [] {"a", "b", "c", "b"}.Contains(str)) { ... }

Check these for more details:

C# syntax shorthand for multiple condition testing

Test for multiple values in an if statement in C#

Gaurang Dave
  • 3,956
  • 2
  • 15
  • 34
  • 2
    As per @Dai, That solution results in an extra heap allocation (for the array) on every call which would hamper performance in tight-loop scenarios. – Saadi May 10 '18 at 08:47
  • @Saadi: Sure, but it's really easy to extract that out into a static variable instead, so it's only allocated once. – Jon Skeet May 10 '18 at 08:51
  • @Saadi you can declare that array at class level if values are fixed and also if values are coming from service or DB, you can initialize it at one time in class. Then there will not be any allocation on every call. Just think in detail before you ask this. This is not that major issue. Now a day, all PC/Laptop got good amount of memory and the scenario you put in question is not of enterprise level application where you need to take care of many things. Hope you get what I mean. – Gaurang Dave May 10 '18 at 08:52
  • Who and why downvoted? I really want to know reason of it. Its find it not removing downvote. Give me a proper reason. – Gaurang Dave May 10 '18 at 08:52
  • Writing the code is one thing. Maintaining and reading it later is another thing. I would prefer to keep things easy to understand and readable. I would prefer having multiple `str` over a new array. (I did not downvote) – Myrtle May 10 '18 at 08:53
  • So we have to create new array every time while checking using such `if` condition !! – Amit May 10 '18 at 09:04
1

You cannot do this.

Normally in if statement you check for Boolean type.

so if(str == "a") this checks str == "a" and returns true or false based on value of str.

but when you are doing

if(str == "a" || "b" || "c" || "d")

what you are actually doing if( (str == "a") || ("b") || ("c") || ("d") )

In this instruction, first part is returning Boolean, but others are not returning Boolean, which is not valid for if statement.

same way you cannot do following too.

int a = 5, b = 6, c = 7;
if(a == b == c) //This is not valid statement
{
}

here it will be

((a == b) == c) ,and (a == b) results into true or false. so in second part check will be,

(true/false == c) , here you cannot compare Boolean with int and cannot provide Boolean result.

I want to do some something similar to if(str == "a" || "b" || "c" || "d") or if (str in {"a", "b", "c", "d"})

as we have seen, we can't do if(str == "a" || "b" || "c" || "d").

But if you have liberty to have a List of strings which does carry all string elements. You need to create a class level (global) list and check in if condition, you can do as below.

lets say listOfElements is such list you have.

if(listOfElements.Contains(str))

Amit
  • 1,821
  • 1
  • 17
  • 30