将ArrayPool与引用类型一起使用的正确方法是什么?
我以为它将充满用默认构造函数“更新”的对象。
例如,在下面的代码中,Foobars当您第一次从ArrayPool中租用时,所有的都为null。
2个问题:
由于返回的对象.Rent最初都是null,因此是否需要先用初始化的对象填充数组池?
返回租用的物品时,我需要清除每个物品吗?例如,foobar.Name = null; foobar.Place = null等等。
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}");
}
}
}
Run Code Online (Sandbox Code Playgroud)