Home
Map
ArrayPool ExampleUse ArrayPool and its Shared property to call Rent to get reusable arrays and reduce allocation.
C#
This page was last reviewed on Dec 7, 2022.
ArrayPool. Sometimes in C# programs we want to allocate many arrays of similar lengths. These arrays may be needed only for short times.
Array
To reuse an array from an ArrayPool, we "rent" it with the Rent() method. We can then Return() the same array to allow later uses.
An example. Here we introduce a method called UseSharedArray that uses the ArrayPool generic type. We call UseSharedArray 3 times with a different number.
Generic
Part 1 We access the ArrayPool type and specify a type parameter int. We use the Shared property, and call Rent.
int, uint
Part 2 The rented array always has a length of at least the value specified (in this program, 5). We use the array here.
Array Length
Part 3 We call Return() to allow the rented array to be used by another call. This enables reuse of allocated arrays.
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
Static array. Sometimes we have simple array sharing requirements—only 1 array may be needed. We can just use a static array of a certain length.
static Array
using System; class Program { static int[] _shared = new int[5]; static void UseStaticArray(int x) { var rentedArray = _shared; 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]); } } static void Main() { UseStaticArray(1); UseStaticArray(2); UseStaticArray(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
A summary. For complex array sharing requirements, ArrayPool is a good choice. For simple programs where only one array is shared, a static array can be used.
static
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 7, 2022 (simplify).
Home
Changes
© 2007-2024 Sam Allen.