一、Array的ConstrainedCopy方法 msdn查看
public static void ConstrainedCopy (Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length);
使用它你可以在任意地方做拷貝
1、舉例:
原數組
int[] a = new int[5] { 1, 3, 5, 4, 2 };
取前3位
int[] b = new int[3];//新數組
Array.ConstrainedCopy(a, 0, b, 0, 3);
//b現在存的就是 {1, 3, 5}
a就是原數組
第一個0就是原數組取值的起始位置
b就是目標數組
第二個0就是目標數組存值的起始位置
3就是存取的長度
二、Array的Resize方法(將一維數組的元素數更改為指定的新大小。)msdn查看
public static void Resize<T> (ref T[]? array, int newSize);
1、舉例:
原數組
int[] a = new int[5] { 1, 3, 5, 4, 2 };
取前3位
Array.Resize(ref a, 3);
//a現在存的就是 {1, 3, 5}