15

Possible Duplicate:
C# if statements matching multiple values

I often find myself writing code where a variable can be either A or B, for example when I call OnItemDataBound on a repeater:

protected void repeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {}
}

I then often think, there must be a simpler way of doing this. I would like to write something like:

if(x == (1 || 2))

SQL has the IN(..) operator, is there something similar in C#?

WHERE x IN(1,2)

I know I could use a switch-statement instead, but thats not simple enought. I want it to be done in an If statement, if possible.

Community
  • 1
  • 1
Jimmy Mattsson
  • 2,085
  • 5
  • 19
  • 34
  • 1
    The most readable way: `if`. The fastest way: probably `switch`. The syntax that solves the problem: COBOL :D – Kendall Frey Jan 05 '12 at 13:50
  • `if (e.Item.DataItem == null) return;` instead of `if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)` is alot easier to write when it comes to bind repeaters. – Jimmy Mattsson Jul 02 '13 at 06:48

6 Answers6

15

I think it is fine as-is; however, you could do something like:

// note the array is actually mutable... just... don't change the contents ;p
static readonly ListItemType[] specialTypes =
     new[]{ListItemType.Item, ListItemType.AlternatingItem};

and check against:

if(specialTypes.Contains(e.Item.ItemType)) {
    // do stuff
}

But to emphasise: I'd actually just use a switch here, as switch on integers and enums has special IL handling via jump-tables, making it very efficient:

switch(e.Item.ItemType) {
    case ListItemType.Item:
    case ListItemType.AlternatingItem:
       // do stuff
       break;
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    +1 for the switch statement. I've used this in the past, generally when I was handling more than the Item or AlternatingItem, such as Footer or Header, but I've just used it as it was still cleaner sometimes than writing the if statement – Anthony Shaw Jan 05 '12 at 13:55
12

You could write an extension method like this:

public static bool In<T>(this T x, params T[] values)
{
  return values.Contains(x);
}

And call it like this:

1.In(2,3,4)

But I would say it's not worth the effort.

void
  • 881
  • 3
  • 20
  • 27
8

If you want to mimic the SQL IN statement you could do something like this...for the simple case of having 2 items, this probably isn't simpler, but for more items, it certainly would be.

(new[] { 1, 2 }).Contains(x);
Steve Danner
  • 21,818
  • 7
  • 41
  • 51
1

You can use the following Method, found in this Answer

public static bool In<T>(this T source, params T[] list)
{
  if(null==source) throw new ArgumentNullException("source");
  return list.Contains(source);
}

Call like this:

if(x.In(1,2,4))
{
      // ...
}
Community
  • 1
  • 1
richn
  • 164
  • 2
  • 9
0

Unless there is no too much possible options for single if, your code is readable and clear, which is most important.

If you often meet if with more the 3 condition, you can use

new List<..>{ condition1, condition2, ... ConditionN}.Any<>().

Something like that.

Tigran
  • 61,654
  • 8
  • 86
  • 123
0

I think that is as simple as you are going to get. Notice how the other answers or even your own suggestion actually use special constructs and workarounds just to shorten some trivial bit of syntax. Also, these clever workarounds will hinder performance.

But, for two to three items that use a lot of space, I like to put the conditions on subsequent lines to make the reading a bit easier.

if (x == MyEnum.SomeReallyLongNameThatEatsUpTheLine ||
    x == MyEnum.TheOtherNameThatWastesSpace)
{
// The simplest code.
}

I guess if you had a very long list of possible values the array approach is much better.

A.R.
  • 15,405
  • 19
  • 77
  • 123