a method called UseSharedArray that uses the ArrayPool generic type. We call UseSharedArray 3 times with a different number.
using System;
using System.Buffers;
class Program
{
static void UseSharedArray(int x)
{
// Part 1: reference the ArrayPool and Rent an array.
var shared = ArrayPool<int>.Shared;
var rentedArray = shared.Rent(5);
// Part 2: store values in rented array and print them.
rentedArray[0] = 0;
rentedArray[1] = 1 * x;
rentedArray[2] = 2 * x;
rentedArray[3] = 3 * x;
rentedArray[4] = 4 * x;
// ... Print integers.
for (int i = 0; i < 5; i++)
{
Console.WriteLine(
"ARRAY {0}: {1}", x, rentedArray[i]);
}
// Part 3: return the rented array.
shared.Return(rentedArray);
}
static void Main()
{
UseSharedArray(1);
UseSharedArray(2);
UseSharedArray(3);
}
}
ARRAY 1: 0
ARRAY 1: 1
ARRAY 1: 2
ARRAY 1: 3
ARRAY 1: 4
ARRAY 2: 0
ARRAY 2: 2
ARRAY 2: 4
ARRAY 2: 6
ARRAY 2: 8
ARRAY 3: 0
ARRAY 3: 3
ARRAY 3: 6
ARRAY 3: 9
ARRAY 3: 12