You cannot inside the method. The reason is that you do not know that T can be from within the method. Maybe you can do a little work, but ideally this should be your approach:
public T GetMinimum<T>(T[] array, params T[] ignorables) where T : IComparable<T> { T result = array[0]; //some issue with the logic here.. what if array is empty foreach (T item in array) { if (ignorables.Contains(item) continue; if (result.CompareTo(item) > 0) { result = item; } } return result; }
Now call it:
double[] inputArray = { double.NaN, double.NegativeInfinity, -2.3, 3 }; GetMinimum(inputArray, double.NaN);
If you are sure that only the element is ignored, then you can take only T as the second parameter (possibly as an optional parameter).
Or else in a shorter approach, simply:
inputArray.Where(x => !x.Equals(double.NaN)).Min();
source share