C#动态修改数组维数!

 1 using System;
 2 using System.Text;
 3 namespace ConsoleApplication2
 4 {
 5  class Class1
 6  {
 7   [STAThread]
 8   static void Main(string[] args)
 9   {
10    int[] arr=new int[]{1,2,3};
11    foreach(int x in arr)
12     Console.Write(x+" ");
13    Console.WriteLine();
14    arr=(int[])Redim(arr,5);
15    foreach(int x in arr)
16     Console.Write(x+" ");
17    Console.WriteLine();
18    arr=(int[])Redim(arr,2);
19    foreach(int x in arr)
20     Console.Write(x+" ");
21    Console.WriteLine();
22   }
23   public static Array Redim(Array origArray,int desiredSize)
24   {
25    Type t=origArray.GetType().GetElementType();
26    Array newArray=Array.CreateInstance(t,desiredSize);
27    Array.Copy(origArray,0,newArray,0,Math.Min(origArray.Length,desiredSize));
28    return newArray;
29   }
30  }
31 }
posted @ 2006-08-08 16:52  吴东雷  阅读(1631)  评论(0编辑  收藏  举报