C#: Most elegant way to test if int x is element of a given set?

I use an extension method:

using System.Linq;

...

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

...


if (!x.In(2,3,61,71))
...

You can rename it to IsElementOf if you prefer this name...


Old question, but haven't seen this simple answer:

!new []{2, 3, 61, 71}.Contains(x)

You could use following LinQ method:

var list = new List<int> { 1, 2, 3, 4, 5 };
var number = 3;

if (list.Any(item => item == number))
    //number is in the list

And for the readability you can put it in an extension method:

public static bool IsElementOf(this int n, IEnumerable<int> list)
{
    return list.Any(i => n == i);
}

//usage
if(3.IsElementOf(list)) //in the list