> For the complete documentation index, see [llms.txt](https://kinematicsoup.gitbook.io/reactor/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kinematicsoup.gitbook.io/reactor/api-reference/ks.reactor/ks.reactor.ksobjectpool-1.md).

# Class ksObjectPool\<T>

Namespace: [KS.Reactor](/reactor/api-reference/ks.reactor.md)\
Assembly: KSCommon.dll

An object pool that constructs new objects using the default constructor or a factory delegate.

```csharp
public class ksObjectPool<T> where T : class
```

#### Type Parameters

`T`

Type of object in the pool

#### Inheritance

[object](https://learn.microsoft.com/dotnet/api/system.object) â† [ksObjectPool\<T>](/reactor/api-reference/ks.reactor/ks.reactor.ksobjectpool-1.md)

#### Inherited Members

[object.Equals(object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals\(system-object\)), [object.Equals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals\(system-object-system-object\)), [object.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.object.gethashcode), [object.GetType()](https://learn.microsoft.com/dotnet/api/system.object.gettype), [object.MemberwiseClone()](https://learn.microsoft.com/dotnet/api/system.object.memberwiseclone), [object.ReferenceEquals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.referenceequals), [object.ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring)

## Constructors

### ksObjectPool() <a href="#ks_reactor_ksobjectpool_1__ctor" id="ks_reactor_ksobjectpool_1__ctor"></a>

Constructor that uses the default constructor to create `T` instances.

```csharp
public ksObjectPool()
```

### ksObjectPool(Factory, int) <a href="#ks_reactor_ksobjectpool_1__ctor_ks_reactor_ksobjectpool__0__factory_system_int32" id="ks_reactor_ksobjectpool_1__ctor_ks_reactor_ksobjectpool__0__factory_system_int32"></a>

Constructor that uses a factory

```csharp
public ksObjectPool(ksObjectPool<T>.Factory factory, int limit = -1)
```

#### Parameters

`factory` [ksObjectPool](/reactor/api-reference/ks.reactor/ks.reactor.ksobjectpool-1.md)\<T>.[Factory](/reactor/api-reference/ks.reactor/ks.reactor.ksobjectpool-1/ks.reactor.ksobjectpool-1.factory.md)

Factory to create new instances.

`limit` [int](https://learn.microsoft.com/dotnet/api/system.int32)

Maximum number of objects allowed in the pool. If less than zero, use [DefaultLimit](/reactor/api-reference/ks.reactor/ks.reactor.ksobjectpool.md#KS_Reactor_ksObjectPool_DefaultLimit)

## Properties

### Instance <a href="#ks_reactor_ksobjectpool_1_instance" id="ks_reactor_ksobjectpool_1_instance"></a>

Static instance.

```csharp
public static ksObjectPool<T> Instance { get; }
```

#### Property Value

[ksObjectPool](/reactor/api-reference/ks.reactor/ks.reactor.ksobjectpool-1.md)\<T>

### IsFull <a href="#ks_reactor_ksobjectpool_1_isfull" id="ks_reactor_ksobjectpool_1_isfull"></a>

Has the pool size reached the [Limit](/reactor/api-reference/ks.reactor.md#KS_Reactor_ksObjectPool_601_Limit)?

```csharp
public bool IsFull { get; }
```

#### Property Value

[bool](https://learn.microsoft.com/dotnet/api/system.boolean)

### Limit <a href="#ks_reactor_ksobjectpool_1_limit" id="ks_reactor_ksobjectpool_1_limit"></a>

The maximum number of objects allowed in the pool. Returns [OverrideLimit](/reactor/api-reference/ks.reactor.md#KS_Reactor_ksObjectPool_601_OverrideLimit) if it is greater than or equal to zero, otherwise returns [DefaultLimit](/reactor/api-reference/ks.reactor/ks.reactor.ksobjectpool.md#KS_Reactor_ksObjectPool_DefaultLimit).

```csharp
public int Limit { get; }
```

#### Property Value

[int](https://learn.microsoft.com/dotnet/api/system.int32)

### OverrideLimit <a href="#ks_reactor_ksobjectpool_1_overridelimit" id="ks_reactor_ksobjectpool_1_overridelimit"></a>

If this is greater than or equal to zero, this is used as the [Limit](/reactor/api-reference/ks.reactor.md#KS_Reactor_ksObjectPool_601_Limit) instead of [DefaultLimit](/reactor/api-reference/ks.reactor/ks.reactor.ksobjectpool.md#KS_Reactor_ksObjectPool_DefaultLimit).

```csharp
public int OverrideLimit { get; set; }
```

#### Property Value

[int](https://learn.microsoft.com/dotnet/api/system.int32)

## Methods

### AtomicFetch() <a href="#ks_reactor_ksobjectpool_1_atomicfetch" id="ks_reactor_ksobjectpool_1_atomicfetch"></a>

Fetch an instance of the object from the pool thread-safely. If the pool is empty, then create a new object.

```csharp
public T AtomicFetch()
```

#### Returns

T

instance

### AtomicFetch(out T) <a href="#ks_reactor_ksobjectpool_1_atomicfetch__0" id="ks_reactor_ksobjectpool_1_atomicfetch__0"></a>

Fetches an instance of the object from the pool thread-safely. If the pool is empty, creates a new object.

```csharp
public bool AtomicFetch(out T obj)
```

#### Parameters

`obj` T

The fetched object.

#### Returns

[bool](https://learn.microsoft.com/dotnet/api/system.boolean)

True if the object was fetched from the pool, false if it was a new object.

### AtomicReturn(T) <a href="#ks_reactor_ksobjectpool_1_atomicreturn__0" id="ks_reactor_ksobjectpool_1_atomicreturn__0"></a>

Returns an object to the pool thread-safely.

```csharp
public void AtomicReturn(T obj)
```

#### Parameters

`obj` T

Pool object.

### Create(int) <a href="#ks_reactor_ksobjectpool_1_create_system_int32" id="ks_reactor_ksobjectpool_1_create_system_int32"></a>

Creates new objects and add them to the pool.

```csharp
public void Create(int amount)
```

#### Parameters

`amount` [int](https://learn.microsoft.com/dotnet/api/system.int32)

Amount of objects to create.

### Fetch() <a href="#ks_reactor_ksobjectpool_1_fetch" id="ks_reactor_ksobjectpool_1_fetch"></a>

Fetch an instance of the object from the pool. If the pool is empty, then create a new object.

```csharp
public T Fetch()
```

#### Returns

T

### Fetch(out T) <a href="#ks_reactor_ksobjectpool_1_fetch__0" id="ks_reactor_ksobjectpool_1_fetch__0"></a>

Fetches an instance of the object from the pool. If the pool is empty, creates a new object.

```csharp
public bool Fetch(out T obj)
```

#### Parameters

`obj` T

The fetched object.

#### Returns

[bool](https://learn.microsoft.com/dotnet/api/system.boolean)

True if the object was fetched from the pool, false if it was a new object.

### Return(T) <a href="#ks_reactor_ksobjectpool_1_return__0" id="ks_reactor_ksobjectpool_1_return__0"></a>

Returns an object to the pool. Objects will not be returned if the pool [IsFull](/reactor/api-reference/ks.reactor.md#KS_Reactor_ksObjectPool_601_IsFull).

```csharp
public void Return(T obj)
```

#### Parameters

`obj` T

Pool object.
