CapyKit/Docs/api/CapyKit.Pool-1.md

123 lines
4.1 KiB
Markdown

# <a id="CapyKit_Pool_1"></a> Class Pool<T\>
Namespace: [CapyKit](CapyKit.md)
Assembly: CapyKit.dll
A managed pool of resources. This class provides a thread-safe way to manage a collection of
objects of type <code class="typeparamref">T</code>.
```csharp
public class Pool<T>
```
#### Type Parameters
`T`
The type of objects to be managed by the pool.
#### Inheritance
[object](https://learn.microsoft.com/dotnet/api/system.object) ←
[Pool<T\>](CapyKit.Pool\-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)
#### Extension Methods
[ObjectExtensions.UpdateProperties<Pool<T\>\>\(Pool<T\>, Pool<T\>\)](CapyKit.Extensions.ObjectExtensions.md\#CapyKit\_Extensions\_ObjectExtensions\_UpdateProperties\_\_1\_\_\_0\_\_\_0\_),
[ObjectExtensions.UpdateProperties\(object, object\)](CapyKit.Extensions.ObjectExtensions.md\#CapyKit\_Extensions\_ObjectExtensions\_UpdateProperties\_System\_Object\_System\_Object\_)
## Constructors
### <a id="CapyKit_Pool_1__ctor_System_Int32_"></a> Pool\(int\)
Initializes a new instance of the <xref href="CapyKit.Pool%601" data-throw-if-not-resolved="false"></xref> class with the specified pool size.
```csharp
public Pool(int poolSize)
```
#### Parameters
`poolSize` [int](https://learn.microsoft.com/dotnet/api/system.int32)
The size of the pool.
### <a id="CapyKit_Pool_1__ctor_System_Int32_System_Func__0__"></a> Pool\(int, Func<T\>\)
Initializes a new instance of the <xref href="CapyKit.Pool%601" data-throw-if-not-resolved="false"></xref> class with the specified pool size
and constructor selector.
```csharp
public Pool(int poolSize, Func<T> constructorSelector)
```
#### Parameters
`poolSize` [int](https://learn.microsoft.com/dotnet/api/system.int32)
The size of the pool.
`constructorSelector` [Func](https://learn.microsoft.com/dotnet/api/system.func\-1)<T\>
The constructor selector used to create new instances of <code class="typeparamref">T</code>.
### <a id="CapyKit_Pool_1__ctor_System_Collections_Generic_IEnumerable__0__"></a> Pool\(IEnumerable<T\>\)
Initializes a new instance of the <xref href="CapyKit.Pool%601" data-throw-if-not-resolved="false"></xref> class with the specified collection
of items.
```csharp
public Pool(IEnumerable<T> collection)
```
#### Parameters
`collection` [IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable\-1)<T\>
The collection of <code class="typeparamref">T</code> items with which to seed the pool.
## Methods
### <a id="CapyKit_Pool_1_GetAvailableItem"></a> GetAvailableItem\(\)
Gets the first available item from the pool and sets its lock.
```csharp
public PoolItem<T> GetAvailableItem()
```
#### Returns
[PoolItem](CapyKit.PoolItem\-1.md)<T\>
The first available item from the pool.
### <a id="CapyKit_Pool_1_ReleaseItem_CapyKit_PoolItem__0__"></a> ReleaseItem\(PoolItem<T\>\)
Releases the lock on the specified item and returns it to the pool.
```csharp
public void ReleaseItem(PoolItem<T> item)
```
#### Parameters
`item` [PoolItem](CapyKit.PoolItem\-1.md)<T\>
The item to release.
#### Remarks
This method sets the <xref href="CapyKit.PoolItem%601.Locked" data-throw-if-not-resolved="false"></xref> flag to <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool">false</a> so that
it can be retrieved by <xref href="CapyKit.Pool%601.GetAvailableItem" data-throw-if-not-resolved="false"></xref>.