It, of course, is ineffective in theory as a direct if test, but it is a red herring. The real question is: are you interested?
There are two sides to this question.
- So what if it is slower? If you do not use this in a loop bound to a processor that runs a million times, the difference is purely theoretical. Use everything that makes for more mistakes that are nice to read and maintain.
- So what if it's uglier? Are you going to write this many times? Of course not - if you intend to use it many times, put it in a well-named method and never think about it again.
As for the LINQ approach, it's a little shorter and a bit readable than yours:
if((new[] { null, " ", " ", "\n" }).Contains(x)) ...
You might want to write another extension method that allows you to call this with the translation of the operand positions, for example.
if(x.In(new[] { null, " ", " ", "\n" })) ...
Verdict:. I will go over with LINQ for more than three or so values ββto check if there is no other more obvious way to check these values ββ(for example, in this case IsNullOrWhiteSpace terribly close) and there are no obvious performance implications. Otherwise, if checked and executed.
source share