I want to create an extension method for IEnumerable that calculates the sum of its terms and returns the total value. This method must perform this sum only if type T is a numeric type (double, float, int ...) and this sum must be different if the IEnumerable is a list, a set, or a dictionary.
My attempt to create this method is just below:
public static class IEnumerableTExtensoes
{
public static double Sum<T>(this IEnumerable<T> IEnum)
{
bool isInt = typeof(T) == typeof(int);
bool isFloat = typeof(T) == typeof(float);
bool isDouble = typeof(T) == typeof(double);
if(isInt || isFloat || isDouble)
{
T sum = (double)0;
foreach(T num in IEnum)
{
sum += num;
}
}
return sum;
}
}
That is not working at all.
Any suggestion?