What is the proper way to use ArrayPool with reference types?
I was assuming that it would be full of objects that were just 'newed up' with the default constructor.
For example, in the code below, all the Foobars
are null when you first rent from the ArrayPool.
2 Questions:
Since the objects returned from
.Rent
are initially all null, do I need to fill up the array pool with initialized objects first?When returning the rented objects do I need to clear each object? For example,
foobar.Name = null; foobar.Place = null
etc...
public class Program
{
public class Foobar {
public string Name {get;set;}
public string Place {get;set;}
public int Index {get;set;}
}
public static void Main()
{
ArrayPool<Foobar> pool = ArrayPool<Foobar>.Shared;
var foobars = pool.Rent(5);
foreach(var foobar in foobars) {
// prints "true"
Console.WriteLine($"foobar is null? ans={foobar == null}");
}
}
}
ArrayPool
would be a good way to reduce allocations of objects and was not thinking about minimizing array allocations. This clears things up. – Rufus