ArrayPool
以下是一个示例代码,用于演示如何使用ArrayPool
using System;
using System.Buffers;
public class ArrayPoolExample
{
public static void Main()
{
// 从数组池中获取一个长度为10的int数组
int[] array = ArrayPool.Shared.Rent(10);
// 填充数组内容
for (int i = 0; i < array.Length; i++)
{
array[i] = i;
}
// 打印数组内容
Console.WriteLine("Array Content:");
foreach (int item in array)
{
Console.Write(item + " ");
}
Console.WriteLine();
// 将数组还回池中
ArrayPool.Shared.Return(array);
}
}
运行结果为:
Array Content:
0 1 2 3 4 5 6 7 8 9
这个例子中,我们使用了ArrayPool