public static bool Any<T>(T item, params T[] items)
{
return items.Contains(item);
}
Usage:
if (Any(6, 1, 2, 3, 4, 5, 6, 7))
{
// 6 == 6
}
if (Any("Hi", "a", "cad", "asdf", "hi"))
{
}
else
{
// "Hi" != "hi" and from anything else.
}
Or:
public string[] items = new string[] {"a", "cad", "asdf", "hi"};
...
if (Any("Hi", items))
{
Works just as well.
}
You can also have more advanced comparison. For example, if you wanted:
if (person.Name == p1.Name ||
person.Name == p2.Name ||
person.Name == p3.Name ||
person.Name == p4.Name ||
person.Name == p5.Name || ...)
{
}
You can have:
public static bool Any<T>(T item, Func<T, T, bool> equalityChecker, params T[] items)
{
return items.Any(x => equalityChecker(item, x));
}
And do:
if (Any(person, (per1, per2) => p1.Name == p2.Name, p1, p2, p3, p4, p5, ...)
{
}
EDIT
If you insist, you can make it, of course, an extension method:
public static bool Any<T>(this T item, params T[] items)
{
return items.Contains(item);
}
Usage:
var b = 6.Any(4, 5, 6, 7); // true
And the same logic of adding the keyword "item" in the signature goes for the overload with the equalityChecker.