<p>If I have a variable in C# that needs to be checked to determine if it is equal to one of a set of variables, what is the best way to do this?</p>

<p>I'm not looking for a solution that stores the set in an array. I'm more curious to see if there is a solution that uses boolean logic in some way to get the answer.</p>

<p>I know I could do something like this:  </p>

<pre><code>int baseCase = 5;
bool testResult = baseCase == 3 || baseCase == 7 || baseCase == 12 || baseCase == 5;
</code></pre>

<p>I'm curious to see if I could do something more like this:</p>

<pre><code>int baseCase = 5;
bool testResult = baseCase == (3 | 7 | 12 | 5);
</code></pre>

<p>Obviously the above won't work, but I'm interested in seeing if there is something more succinct than my first example, which has to repeat the same variable over and over again for each test value.</p>

<p><strong>UPDATE:</strong><br>
I decided to accept CoreyN's answer as it seems like the most simple approach. It's practical, and still simple for a novice to understand, I think.</p>

<p>Unfortunately where I work our system uses the .NET 2.0 framework and there's no chance of upgrading any time soon. Are there any other solutions out there that don't rely on the .NET 3.5 framework, besides the most obvious one I can think of:  </p>

<pre><code>new List&lt;int&gt;(new int[] { 3, 6, 7, 1 }).Contains(5);
</code></pre>

[{'answer_id': 18416, 'author': 'Corey', 'author_id': 1595, 'author_profile': 'https://Stackoverflow.com/users/1595', 'pm_score': 5, 'selected': True, 'text': '<pre><code>        bool b = new int[] { 3,7,12,5 }.Contains(5);\n</code></pre>\n'}, {'answer_id': 18423, 'author': 'Jon Galloway', 'author_id': 5, 'author_profile': 'https://Stackoverflow.com/users/5', 'pm_score': 0, 'selected': False, 'text': '<p>I usually use <a href="https://stackoverflow.com/users/1595/coreyn">CoreyN</a>\'s <a href="https://stackoverflow.com/questions/18407/most-succinct-way-to-determine-if-a-variable-equals-a-value-from-a-list-of-valu#18416">solution</a> for simple cases like that. Anything more complex, use a LINQ query.</p>\n'}, {'answer_id': 55082, 'author': 'Craig Tyler', 'author_id': 5408, 'author_profile': 'https://Stackoverflow.com/users/5408', 'pm_score': -1, 'selected': False, 'text': "<p>Since you did not specify what type of data you have as input I'm going to assume you can partition your input into powers of 2 -> 2,4,8,16... This will allow you to use the bits to determine if your test value is one of the bits in the input.</p>\n\n<p>4  => 0000100<br>\n16 => 0010000<br>\n64 => 1000000</p>\n\n<p>using some binary math...  </p>\n\n<p>testList = 4 + 16 + 64 => 1010100<br>\ntestValue = 16<br>\ntestResult = testList &amp; testValue</p>\n"}, {'answer_id': 153037, 'author': 'Joe', 'author_id': 13087, 'author_profile': 'https://Stackoverflow.com/users/13087', 'pm_score': 1, 'selected': False, 'text': "<p>You can do something similar with .NET 2.0, by taking advantage of the fact that an array of T implements IList&lt;T&gt;, and IList&lt;T&gt; has a Contains method.  Therefore the following is equivalent to Corey's .NET 3.5 solution, though obviously less clear:</p>\n\n<pre><code>bool b = ((IList&lt;int&gt;)new int[] { 3, 7, 12, 5 }).Contains(5); \n</code></pre>\n\n<p>I often use IList&lt;T&gt; for array declarations, or at least for passing one-dimensional array arguments.  It means you can use IList properties such as Count, and switch from an array to a list easily.  E.g.</p>\n\n<pre><code>private readonly IList&lt;int&gt; someIntegers = new int[] { 1,2,3,4,5 };\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/18407', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/392/']