How about this? copes with any number and any type, optionally
allowing your own comparer (definition of equality)...
Marc
static void Main()
{
Console.WriteLine(AllEqual("abc", "abc", "abc"));
Console.WriteLine(AllEqual("abc", "def", "abc"));
Console.WriteLine(AllEqual(1, 1, 1, 1, 1));
Console.WriteLine(AllEqual(1, 2, 3, 2, 1));
}
static bool AllEqual<T>(params T[] values)
{
return AllEqual<T>(EqualityComparer<T>.Default, values);
}
static bool AllEqual<T>(IEqualityComparer<T> comparer, params T[]
values)
{
if(values==null) throw new ArgumentNullException("values");
if (comparer == null) throw new
ArgumentNullException("comparer");
int len = values.Length;
if(len<2) throw new InvalidOperationException("2 or more
values required");
T val = values[0];
for (int i = 1; i < len; i++)
{
if (!comparer.Equals(val, values[i]))
{
return false;
}
}
return true;
}
|