C# Generic Extension function to check if an item is in the list of values

Michael Roma

The following is a C# Generic Extension function that checks if an item is in the given list of values.

Below is the function definition:

public static class Extensions
{
    public static bool In(this T item, params T[] checkList)
    {
        return checkList.Contains(item);
    }
}

Here is an example on how to call it:

// example with int
int x = 3;
x.In(1, 2)); // false
x.In(3, 4)); // true

// example with enums
var r = Roles.Student;
r.In(Roles.Student, Roles.Teacher)); // true
r.In(Roles.Contractor, Roles.Teacher)); // false