Simple Extension Method to Evaluate Listings For a Match
I’m currently dealing with a system that switches between different states, which require evaluations that look similar to the snippet below:
//check status flags TestStatus status = GetStatus(); if (status == TestStatus.Preparing || status == TestStatus.Running || status == TestStatus.PostDelay) { ... }
Given the fact that I’m having lots of states, I had to produce pretty nasty code, so I wrote that handy little extension method that takes care of the issue once and for all:
Edit (2009.02.08): Returning false for null values which allows to submit null references without exception.
/// <summary> /// Checks a list of candidates for equality to a given /// reference value. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="value">The evaluated value.</param> /// <param name="candidates">A liste of possible values that are /// regarded valid.</param> /// <returns>True if one of the submitted <paramref name="candidates"/> /// matches the evaluated value. If the <paramref name="candidates"/> /// parameter itself is null, too, the method returns false as well, /// which allows to check with null values, too.</returns> /// <exception cref="ArgumentNullException">If <paramref name="candidates"/> /// is a null reference.</exception> public static bool Is<T>(this T value, params T[] candidates) { if (candidates == null) return false; foreach (var t in candidates) { if (value.Equals(t)) return true; } return false; }
Using this “Is” extension, I can now write the above code like this:
if (status.Is(TestStatus.Preparing, TestStatus.Running, TestStatus.PostDelay)
{
...
}
Of course, this is not limited to Enums but works with everything that supports the Equals operator:
string name = ... if (name.Is("foo", "bar")) { //foobar! }