4using System.Collections.Generic;
5using System.Runtime.CompilerServices;
12 readonly Stack<T> objects =
new Stack<T>();
16 readonly Func<T> objectGenerator;
18 public Pool(Func<T> objectGenerator,
int initialCapacity)
20 this.objectGenerator = objectGenerator;
24 for (
int i = 0; i < initialCapacity; ++i)
25 objects.Push(objectGenerator());
29 [Obsolete(
"Take() was renamed to Get()")]
30 public T Take() => Get();
33 [MethodImpl(MethodImplOptions.AggressiveInlining)]
34 public T Get() => objects.Count > 0 ? objects.Pop() : objectGenerator();
37 [MethodImpl(MethodImplOptions.AggressiveInlining)]
38 public void Return(T item) => objects.Push(item);
41 public int Count => objects.Count;