您的位置:首页 > 编程语言 > C#

C#动态修改数组维数

2007-05-16 11:22 211 查看
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 }

From: /article/5082314.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: