static void Main(string[] args)
        {
            int[] a=new int[]{1,23,4,4,1,2,3,4,5,6,5,4,3,2,1,3,4};            Console.WriteLine("排序前数组");
            for (int j = 0; j < a.Length; j++)
            {
                Console.WriteLine("{0} ", a[j]);            }            Sort.QuickSort0926<int>(a, 0, a.Length - 1);            Console.WriteLine("排序后数组");
            for (int j = 0; j < a.Length; j++)
            {
                Console.WriteLine("{0} ", a[j]);            }
            Console.ReadLine();
        }        public static void QuickSort0926<T>(T[] data,int low,int high)
        {
            if (low > high)
                return;            //分割
            int l = low;
            int h = high;
            //取出比较值
            T pivot=data[low];
            while(l<h)
            {                while (l<h && data[h] >= pivot)
                {
                    h--;
                }
                if (l < h)
                {
                    data[l] = data[h];
                    l++;
                }
                while (l < h && data[l] <= pivot)
                {
                    l++;
                }
                if (l < h)
                {
                    data[h] = data[l];
                    h--;
                }
            }
        
            data[l] = pivot;            //递归
            QuickSort0926(data, low, l - 1);
            QuickSort0926(data, l + 1, high);        }编译时会报错,提示:   错误 1 运算符“>=”无法应用于“T”和“T”类型的操作数
                      错误 2 运算符“<=”无法应用于“T”和“T”类型的操作数请问我这中泛型用法有问题吗?

解决方案 »

  1.   


    public static void QuickSort0926<T>(T[] data,int low,int high)
    这那里是范性呀,明明就是名字叫QuickSort0926<T>的方法,方法的名字中是不能带<>符号的.好好再看看范型的定义吧..
      

  2.   

    list<T>
    这才叫泛型 方法名都不会写<>这种符号都拿出来 不错才怪
      

  3.   

    晕倒,楼上的在误人子弟吧,当然可以写成void method<T>()啦,我都写了好几百年了
    我猜,List<>的原型应该是class List<T>{}
      

  4.   

    你有没有搞错!你从来没听说过一中泛型概念叫泛型方法吗?看来CSDN上的等级真的不是等级啊!
    说话那么冲!
    即使这样你把QuickSort0926独立成泛型类中的方法一样也过不了编译!唉!没人知道吗?
      

  5.   

    泛型的当然不能直接用 >=, <=  比较每一种数据类型的比较方法都不一样的你可以像 .net framework 一样, 实现一个 IComparer 接口