数组自定义索引

来源:互联网 发布:用ansys有限元软件 编辑:程序博客网 时间:2024/05/16 15:58

一维数组自定义索引         

            int[] len=new int[]{4};
            int[] bound = new int[]{2};
            Array array =Array.CreateInstance(typeof(int), len, bound);

           Console.WriteLine(array.GetValue(2));

           int[]  ar =(int[])Array.CreateInstance(typeof(int), len, bound);//出错,无法转换 why

           解决int[] bound = new int[]{0}; //索引从0开始

         Array是一个数组,这样是可以。但是注意:int[]这种形式的(下表形式),必须是0

二维数组自定义索引   

            int[] lowerBounds = new int[] { 3, 10 };

            int[]  length = new int[] { 2, 3 };

 

            int[,] arr = (int[,])Array.CreateInstance(typeof(int), length, lowerBounds);  //  OK

CLR的数组中“以0开始的一维数组”是最快的也是IL直接支持的!C#中的xxx[]类型就是代表这个类型的。而以非0开始的一维数组是不属于这类的,因此类型是xxx[*]。所以他们之间不能转换。至于多维数组,不管是不是以0开始,很显然也不属于这类的,因此xxx[,]总可以被转换。

参考阅读:http://msdn.microsoft.com/en-us/magazine/cc301755.aspx

博文扩展:http://www.cnblogs.com/mgen/archive/2011/07/31/2123218.html#commentform