Home
C#
ArrayPool Example
Updated Apr 18, 2025
Dot Net Perls
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.
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
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
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 pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Apr 18, 2025 (edit).
Home
Changes
© 2007-2025 Sam Allen