using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            foo(new string[] { });
            foo(new int[] { });
        }
        static void foo(object arr)
        {
            Console.WriteLine(arr.GetType());
        }
    }
}

解决方案 »

  1.   

    返回的是System.Int32[],判断的时候if(arr.GetType()== System.Int32[])报错了
      

  2.   

    你好好看看 List<T> 的 Sort 方法的源代码吧。如果你想学习.net程序开发却不读 .net framework 源代码,这就纯粹是把自己当作局外人了。
      

  3.   

    给你写个例子(虽然我知道你可能看不懂,但是最差也只能点评到这个地步,实在没有办法再给你写一个比这个更差的例子了)
            private static void SelectSort<T>(T[] arr) where T : IComparable<T>
            {
                SelectSort(arr, 0, arr.Length - 1);
            }        private static void SelectSort<T>(T[] arr, int start, int end) where T : IComparable<T>
            {
                while (start <= end)
                {
                    var min = start;
                    for (var i = start + 1; i <= end; i++)
                        if (arr[i].CompareTo(arr[min]) < 0)
                            min = i;
                    if (min != start)
                        Swap(arr, start, min);
                    start++;
                }
            }
    这样,各种各样的实现了 IComparable<T> 接口的对象(整数和字符串都实现了它)就可以调用它。如果你想自定义自己的比大小规则,你仅仅需要将比较大小方法作为方法参数传入,或者将对象封装到自定义的类型中(并且实现比大小接口),仍然可以调用这个排序程序。这里要强调的不是你能抄写什么代码,而是你能理解什么c#程序设计知识!
      

  4.   

    缺少一个 Swap 方法,补上         private static void Swap<T>(T[] arr, int start, int end)
            {
                var m = arr[start];
                arr[start] = arr[end];
                arr[end] = m;
            }
    例如我们可以写一个测试            var arr1 = new int[] { 2, 38, 27, 223, 28 };
                var arr2 = new string[] { "adsfjass", "2384skdfs", "_(&&we2" };
                SelectSort(arr1);
                SelectSort(arr2);
    运行上面4行代码之后,你就发现两个数组都被 SelectSort 方法排好顺序了。学点泛型知识吧。
      

  5.   

    if (arr.GetType().ToString() == "System.Int32[]")
    orif (arr.GetType() == typeof(System.Int32[]))
      

  6.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                foo(new string[] { });
                foo(new int[] { });
            }
            static void foo(object arr)
            {
                Console.WriteLine(arr.GetType());
            }
        }
    }
      

  7.   

    学习一下net framework 源代码
      

  8.   

    我是做java的,只不过刚开始,C#只是选修课修学分
      

  9.   

    真要这么奇葩地排序也好办Sort<T>(T[] array, bool desc);Sort<T>(T[] array)
    {
        if(T is string)
            Sort(array, true);
        else
            Sort(array, false);
    }
      

  10.   

    上面应该是 if(typeof(T) == typeof(string)) 才对
      

  11.   


    你这个方法不行。人家楼主要的是整形正排序, 字符串反排序。而你写的这个方法,是如何根据类型自己的IComparable排序,和楼主要求不符啊。