C#怎么判断数组对象类型?

esc_ai
博客专家认证
2013-12-22 11:00:00
写个msort方法,参数为整数数组时将数组降序输出;参数为字符串时,降字符串反序输出。
数 组做参数的时候怎么判断数组的类型, 实在没思路, 求指导

...全文
775 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
普世编程技术 2013-12-25
  • 打赏
  • 举报
回复
引用 5 楼 sp1234 的回复:
缺少一个 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 方法排好顺序了。 学点泛型知识吧。
你这个方法不行。 人家楼主要的是整形正排序, 字符串反排序。 而你写的这个方法,是如何根据类型自己的IComparable排序,和楼主要求不符啊。
种草德鲁伊 2013-12-25
  • 打赏
  • 举报
回复
上面应该是 if(typeof(T) == typeof(string)) 才对
种草德鲁伊 2013-12-25
  • 打赏
  • 举报
回复
真要这么奇葩地排序也好办


Sort<T>(T[] array, bool desc);

Sort<T>(T[] array)
{
    if(T is string)
        Sort(array, true);
    else
        Sort(array, false);
}

esc_ai 2013-12-25
  • 打赏
  • 举报
回复
引用 4 楼 sp1234 的回复:
给你写个例子(虽然我知道你可能看不懂,但是最差也只能点评到这个地步,实在没有办法再给你写一个比这个更差的例子了)

        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#程序设计知识!
我是做java的,只不过刚开始,C#只是选修课修学分
esc_ai 2013-12-25
  • 打赏
  • 举报
回复
引用 5 楼 sp1234 的回复:
缺少一个 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 方法排好顺序了。 学点泛型知识吧。
字符串时时反序输出的,我觉得这样太复杂了
种草德鲁伊 2013-12-23
  • 打赏
  • 举报
回复
sp1234的是正解哈哈
devmiao 2013-12-23
  • 打赏
  • 举报
回复
if (arr.GetType().ToString() == "System.Int32[]") or if (arr.GetType() == typeof(System.Int32[]))
devmiao 2013-12-23
  • 打赏
  • 举报
回复
引用 2 楼 napoay 的回复:
[quote=引用 1 楼 devmiao 的回复:] 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()); } } }
返回的是System.Int32[],判断的时候if(arr.GetType()== System.Int32[])报错了[/quote] 因为你忘记打引号了!
  • 打赏
  • 举报
回复
缺少一个 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 方法排好顺序了。 学点泛型知识吧。
  • 打赏
  • 举报
回复
给你写个例子(虽然我知道你可能看不懂,但是最差也只能点评到这个地步,实在没有办法再给你写一个比这个更差的例子了)

        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#程序设计知识!
MR00009 2013-12-23
  • 打赏
  • 举报
回复
泛型,还蛮重要的,很实用。
小生我怕怕 2013-12-23
  • 打赏
  • 举报
回复
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()); } } }
  • 打赏
  • 举报
回复
你好好看看 List<T> 的 Sort 方法的源代码吧。 如果你想学习.net程序开发却不读 .net framework 源代码,这就纯粹是把自己当作局外人了。
esc_ai 2013-12-22
  • 打赏
  • 举报
回复
引用 1 楼 devmiao 的回复:
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()); } } }
返回的是System.Int32[],判断的时候if(arr.GetType()== System.Int32[])报错了
devmiao 2013-12-22
  • 打赏
  • 举报
回复
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()); } } }

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧