using System.Collections.Generic; namespace Avalonia.Threading { public class ThreadSafeObjectPool where T : class, new() { private Stack _stack = new Stack(); public static ThreadSafeObjectPool Default { get; } = new ThreadSafeObjectPool(); public T Get() { lock (_stack) { if(_stack.Count == 0) return new T(); return _stack.Pop(); } } public void Return(T obj) { lock (_stack) { _stack.Push(obj); } } } }