Is there a shorter way of writing something like this:
if(x==1 || x==2 || x==3) // do something
What I'm looking for is something like this:
if(x.in((1,2,3)) // do something
Is there a shorter way of writing something like this:
if(x==1 || x==2 || x==3) // do something
What I'm looking for is something like this:
if(x.in((1,2,3)) // do something
This answer refers to a possible future version of C# ;-) If you consider switching to Visual Basic, or if Microsoft finally decides to introduce the Select Case statement to C#, it would look like this:
If it's in an
IEnumerable<T>
, use this:Else, here's a short extension method:
Put it in a
static class
, and you can use it like this:You can create a simple
Dictionary<TKey, TValue>
that'll be used as a Decision Table for that problem:Once you've initialized your
Dictionary
, all left to do is:decisionTable[theNumberToTest]();
You could achieve this by using the List.Contains method:
Required reading: MSDN Extension methods