using System.Collections.Generic; namespace AibisDream.Kit { public static class ListPool { /// /// 栈对象:存储多个List /// static readonly Stack> ListStack = new(8); /// /// 出栈:获取某个List对象 /// /// public static List Get() { return ListStack.Count == 0 ? new List(8) : ListStack.Pop(); } /// /// 入栈:将List对象添加到栈中 /// /// public static void Release(List toRelease) { if (ListStack.Contains(toRelease)) { throw new System.InvalidOperationException( "重复回收 List,The List is released even though it is in the pool"); } toRelease.Clear(); ListStack.Push(toRelease); } } public static class ListPoolExtensions { public static void Release2Pool(this List self) { ListPool.Release(self); } } }