ObjectPool<T>

Implements IObjectPool<T>, IDisposable


Declaration

public sealed class ObjectPool<T> : IObjectPool<T> where T : class

Description

Reuses objects from a shared pool to prevent instantiating new objects. The object pool can have a set capacity or it can grow in size. Optionally, objects that are currently active can be reused when the pool has reached capacity.

Type Parameters

TThe type of object to pool.

Properties

poolThe list of all objects waiting to be reused (Read only).
activeItemsThe list of objects currently being used (Read only).
activeCountThe number of objects currently being used (Read only).
availableCountThe number of objects available to be reused (Read only).
maxCapacityThe maximum number of objects that can be generated.
reuseActiveWhether active objects should be reused when the object pool has reached capacity.
generatorThe function that generates a new object.

Constructors

ObjectPoolCreates a new object pool with an initial capacity. New objects are created as needed using the object type default value.

Methods

DisposeDisposes of all class resources.
RetrieveRemoves and returns an object from the pool. If there are no available objects in the pool, either a new object will be generated or the oldest object will be reused depending upon how the object pool is configured.
RecycleAdds an object back to the pool so it can be reused.
EmptyEmpties the pool of all objects.