I just wrote an if statement in the lines of
if (value == value1 || value == value2 || value == value3 || value == value4)
//do something
and got annoyed that I always have to repeat the 'value ==' part. In my opinion this is serving no purpose other than making it difficult to read.
I wrote the following ExtensionMethod that should make above scenario more readable:
public static bool IsEqualToAny<T>(this T value, params T[] objects)
{
return objects.Contains(value);
}
Now I could simply write
if (value.IsEqualToAny(value1, value2, value3, value4))
//do something
Is this a good usage of an ExtensionMethod?
EDIT:
Thanks for all the great answers. For the record: I have kept the method. I simply prefer reading this kind of if statement from left to right (if this value is equal to any of these instead of if these values contain this value). Having one more method on each object is not an issue for me.