0

Basically I have an object which for the purpose of this question I am calling "a". If property "b" of object "a" is any one of 1, 4, 6 (for example but there could be more values that are appropriate) I want to set it to 8 (again for example). Now typically I would end up doing something like -

if( a.b == 1 || a.b == 4 || a.b == 6 ) 
    a.b = 8;

But I can't help thinking that there must be something simpler/shorter. I'm really looking for something like MSSQL in syntax (if it exists) -

SELECT
  *
FROM table
WHERE
  table.field IN (1, 4, 6)

I suppose I could create an array, populate it and then check if it contains my values in the if statement but that is just going to result in pretty much the same amount of code than the original ifstatement and creating an array variable just for the if feels a little overkill -

int[] values = { 1, 4, 6 };
if(values.Contains(a.b))
    a.b = 8;
John C
  • 3,052
  • 3
  • 34
  • 47
  • In these cases I use the array approach - is makes the intent of the code much clearer. – SWeko Nov 20 '15 at 10:31
  • 3
    It's hard to beat 3 logical ops for readability or speed. – H H Nov 20 '15 at 10:32
  • 4
    Any alternative would be less readable. I would leave it as is. – Konamiman Nov 20 '15 at 10:33
  • It's not "overkill" to create an small array. It's all about readability and maintainability. – Tim Schmelter Nov 20 '15 at 10:34
  • 2
    Here's another duplicate: http://stackoverflow.com/questions/3907299/if-statements-matching-multiple-values – Tim Schmelter Nov 20 '15 at 10:35
  • @StephenBrickner - that will give the wrong results. – H H Nov 20 '15 at 10:35
  • One alternative would be to write an extension method on the type of `a.b` that takes a parameter array, called for example `EqualOneOf`. – MicroVirus Nov 20 '15 at 10:36
  • @Henk, how so? It equates to the exact same if statement. – Stephen Brickner Nov 20 '15 at 10:37
  • No it doesn't. Try it. – H H Nov 20 '15 at 10:38
  • @wudzik Wouldn't [if statements matching multiple values](http://stackoverflow.com/questions/3907299/if-statements-matching-multiple-values) be a more authorative duplicate match? – MicroVirus Nov 20 '15 at 10:38
  • @StephenBrickner, your | perform a bitwise or, [| Operator (C# Reference)](https://msdn.microsoft.com/en-us/library/kxszd0kx.aspx) – Gian Paolo Nov 20 '15 at 10:38
  • Why do want to take U turn when there is a straight path. `If` and multiple `||` is the best way. Others will just look ugly or require you take extra efforts, which does not make sense. – Nikhil Vartak Nov 20 '15 at 10:40
  • @wudzik Ahh well, doesn't really matter that much, anyway. – MicroVirus Nov 20 '15 at 10:44
  • @HenkHolterman, @dotnetkid I realise that with just three checks nothing much is gained however in the past I've occasionally seen `if` statements taking up multiple lines with all the possible values (i.e. `if(a.b == 1 || a.b == 3 || ... || ... || ... || ... || ...|| ... || ...` you get the idea) and always thought that there must be a better way just not taken the time to investigate. The code I'm working with right now has five possible values (although I've come up with a cleaner way of handling it while typing this comment) so I thought it was worth investigating. – John C Nov 20 '15 at 11:03
  • There is a middle ground here, but when the number of terms gets that large there usually is a deeper problem. – H H Nov 20 '15 at 11:14
  • @HenkHolterman Yeah, in places where I've seen such long `if` statements none of the code has been particularly *good*. I've actually reduced the code in my `if` to one check of a boolean property instead of checking the int property's value multiple times. – John C Nov 20 '15 at 11:28

1 Answers1

3

Answering your question about MSSQL IN similiar syntax, the shortest thing you can do is:

if (new[] {1, 4, 6}.Contains(a.b)) {
    a.b = 8;
}

However, this code may bring more problems in readability, than improvements.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
  • 2
    But it's not really shorter than the original code. It will be (a lot) slower. – H H Nov 20 '15 at 10:33
  • This looks shorter but in fact the difference is not big for 3 values. On the other hand, it is less readable than simple `if`. – i486 Nov 20 '15 at 10:33
  • readability goes for a toss here....... – Viru Nov 20 '15 at 10:34
  • +1 because for three values it is not worth it, but the OP says "there could be more values that are appropriate" and if the list grows this is better than an endless chain of `||`s. – Konamiman Nov 20 '15 at 10:36
  • 1
    In VB.NET it's even shorter: `Ìf {1,4,6}.Contains(a.b) Then a.b=8` – Tim Schmelter Nov 20 '15 at 10:36
  • 2
    @Konamiman if you have an endless chain of ORs then you "might" be working at the wrong level of abstraction, and the correct response would not be to make the chain take up less screen space. – Jon Nov 20 '15 at 10:55