Can I achieve
if (a == "b" || "c")
instead of
if (a == "b" || a== "c")
?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, you can do:
if you have the LINQ extensions available, but that’s hardly an improvement.
In response to the comment about performance, here’s some basic timing code. Note that the code must be viewed with a critical eye, I might have done things here that skew the timings.
The results first:
All the code was executed twice, and only pass nr. 2 was reported, to remove JITting overhead from the equation. Both passes executed each type of check one million times, and executed it both where the element to find was one of the elements to find it in (that is, the if-statement would execute its block), and once where the element was not (the block would not execute). The timings of each is reported. I tested both a pre-built array and one that is built every time, this part I’m unsure how much the compiler deduces and optimizes away, there might be a flaw here.
In any case, it appears that using a switch-statement, with or without interning the string first, gives roughly the same results as the simple or-statement, which is to be expected, whereas the array-lookup is much more costly, which to me was also expected.
Please tinker with the code, and correct (or comment) it if there’s problems.
And here’s the source code, rather long: