本文整理匯總了C#中System.Array.Resize<T>方法的典型用法代碼示例。如果您正苦於以下問題:C# Array.Resize<T>方法的具體用法?C# Array.Resize<T>怎麽用?C# Array.Resize<T>使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Array
的用法示例。
在下文中一共展示了Array.Resize<T>方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
using System;
public class SamplesArray
{
public static void Main() {
// Create and initialize a new string array.
String[] myArr = {"The", "quick", "brown", "fox", "jumps",
"over", "the", "lazy", "dog"};
// Display the values of the array.
Console.WriteLine(
"The string array initially contains the following values:");
PrintIndexAndValues(myArr);
// Resize the array to a bigger size (five elements larger).
Array.Resize(ref myArr, myArr.Length + 5);
// Display the values of the array.
Console.WriteLine("After resizing to a larger size, ");
Console.WriteLine("the string array contains the following values:");
PrintIndexAndValues(myArr);
// Resize the array to a smaller size (four elements).
Array.Resize(ref myArr, 4);
// Display the values of the array.
Console.WriteLine("After resizing to a smaller size, ");
Console.WriteLine("the string array contains the following values:");
PrintIndexAndValues(myArr);
}
public static void PrintIndexAndValues(String[] myArr) {
for(int i = 0; i < myArr.Length; i++)
{
Console.WriteLine(" [{0}] : {1}", i, myArr[i]);
}
Console.WriteLine();
}
}
輸出:
The string array initially contains the following values: [0] : The [1] : quick [2] : brown [3] : fox [4] : jumps [5] : over [6] : the [7] : lazy [8] : dog After resizing to a larger size, the string array contains the following values: [0] : The [1] : quick [2] : brown [3] : fox [4] : jumps [5] : over [6] : the [7] : lazy [8] : dog [9] : [10] : [11] : [12] : [13] : After resizing to a smaller size, the string array contains the following values: [0] : The [1] : quick [2] : brown [3] : fox
示例2: Main
//引入命名空間
using System;
public class Example
{
public static void Main()
{
int[,] arr = new int[10,2];
for (int n1 = 0; n1 <= arr.GetUpperBound(0); n1++) {
arr[n1, 0] = n1;
arr[n1, 1] = n1 * 2;
}
// Make a 2-D array larger in the first dimension.
arr = (int[,]) ResizeArray(arr, new int[] { 12, 2} );
for (int ctr = 0; ctr <= arr.GetUpperBound(0); ctr++)
Console.WriteLine("{0}: {1}, {2}", ctr, arr[ctr, 0], arr[ctr, 1]);
Console.WriteLine();
// Make a 2-D array smaller in the first dimension.
arr = (int[,]) ResizeArray(arr, new int[] { 2, 2} );
for (int ctr = 0; ctr <= arr.GetUpperBound(0); ctr++)
Console.WriteLine("{0}: {1}, {2}", ctr, arr[ctr, 0], arr[ctr, 1]);
}
private static Array ResizeArray(Array arr, int[] newSizes)
{
if (newSizes.Length != arr.Rank)
throw new ArgumentException("arr must have the same number of dimensions " +
"as there are elements in newSizes", "newSizes");
var temp = Array.CreateInstance(arr.GetType().GetElementType(), newSizes);
int length = arr.Length <= temp.Length ? arr.Length : temp.Length;
Array.ConstrainedCopy(arr, 0, temp, 0, length);
return temp;
}
}
輸出:
0: 0, 0 1: 1, 2 2: 2, 4 3: 3, 6 4: 4, 8 5: 5, 10 6: 6, 12 7: 7, 14 8: 8, 16 9: 9, 18 10: 0, 0 11: 0, 0 0: 0, 0 1: 1, 2