当前位置:编程学习 > C#/ASP.NET >>

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

    写个msort方法,参数为整数数组时将数组降序输出;参数为字符串时,降字符串反序输出。
   数 组做参数的时候怎么判断数组的类型,   实在没思路, 求指导
 
          --------------------编程问答-------------------- 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 楼 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[])报错了 --------------------编程问答-------------------- 你好好看看 List<T> 的 Sort 方法的源代码吧。

如果你想学习.net程序开发却不读 .net framework 源代码,这就纯粹是把自己当作局外人了。

--------------------编程问答-------------------- 给你写个例子(虽然我知道你可能看不懂,但是最差也只能点评到这个地步,实在没有办法再给你写一个比这个更差的例子了)


        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#程序设计知识! --------------------编程问答-------------------- 缺少一个 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 方法排好顺序了。

学点泛型知识吧。 --------------------编程问答--------------------
引用 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[])报错了


因为你忘记打引号了! --------------------编程问答-------------------- if (arr.GetType().ToString() == "System.Int32[]")
or

if (arr.GetType() == typeof(System.Int32[])) --------------------编程问答-------------------- sp1234的是正解哈哈 --------------------编程问答-------------------- 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());
        }
    }
} --------------------编程问答-------------------- 泛型,还蛮重要的,很实用。 --------------------编程问答-------------------- 学习一下net framework 源代码 --------------------编程问答--------------------
引用 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 方法排好顺序了。

学点泛型知识吧。
字符串时时反序输出的,我觉得这样太复杂了 --------------------编程问答--------------------
引用 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#只是选修课修学分 --------------------编程问答-------------------- 真要这么奇葩地排序也好办



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

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

--------------------编程问答-------------------- 上面应该是 if(typeof(T) == typeof(string)) 才对 --------------------编程问答--------------------
引用 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排序,和楼主要求不符啊。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,