C#泛型方法

 //数组排序,泛型约束写法
        public static void sort<T>(T[] list) where T : System.IComparable<T>
        {
            T currentMin;
            int currentMinIndex;
            for (int i = 0; i < list.Length; i++)
            {
                currentMin = list[i];
                currentMinIndex = i;
                for (int j = i + 1; j < list.Length; j++)
                {
                    if (currentMin.CompareTo(list[j]) > 0)
                    {
                        currentMin = list[j];
                        currentMinIndex = j;
                    }
                }
                if (currentMinIndex != i)
                {
                    list[currentMinIndex] = list[i];
                    list[i] = currentMin;
                }
            }
        }

猜你喜欢

转载自blog.csdn.net/shuyizhi/article/details/78530055