Как сравнить универсальный тип T?

Везде, где я находил ответы, T объявлялся в классе, но у меня он объявляется в методе:

        public static List<T> Max<T>(this List<T> List, int start = 0, int end = 0)
        {
            if (end == 0) end = List.Count-1;

            T Max = List[0];
            for (int i = 1; i < end; i++)
            {

                if (List[i] > Max) Max = List[i]; //">" невозможно применить к операнду типа "T" и "T"
            }

            return Max;
        }

И поэтому прилепить : where T : IComparable<T> я не могу (пробовал, никуда не прилепляется). Как поступать в таком случае?


Ответы (2 шт):

Автор решения: aepot
public static class ListExtensions
{
    public static T Max<T>(this List<T> list, int startIndex = 0, int endIndex = -1) where T : IComparable<T>
    {
        if (endIndex == -1)
            endIndex = list.Count - 1;

        T max = list[0];
        for (int i = startIndex; i <= endIndex; i++)
        {
            if (list[i].CompareTo(max) > 0)
                max = list[i];
        }

        return max;
    }
}
static void Main(string[] args)
{
    List<int> list = new List<int> { 5, 6, 2, 3, 4, 0 };
    int max = list.Max();
    Console.WriteLine(max);
    Console.ReadKey();
}
6
→ Ссылка
Автор решения: KuzCode
    public static T Max<T>(this List<T> List, int start = 0, int end = 0) where T : IComparable<T>
    {
        if (end == 0) end = List.Count - 1;

        T Max = List[0];
        for (int i = 1; i < end; i++)
        {

            if (List[i].CompareTo(Max) > 0) Max = List[i];
        }

        return Max;
    }
→ Ссылка