45 lines
1.2 KiB
C#
45 lines
1.2 KiB
C#
using System.Collections.Generic;
|
||
|
||
namespace AibisDream.Kit
|
||
{
|
||
public static class ListPool<T>
|
||
{
|
||
/// <summary>
|
||
/// 栈对象:存储多个List
|
||
/// </summary>
|
||
static readonly Stack<List<T>> ListStack = new(8);
|
||
|
||
/// <summary>
|
||
/// 出栈:获取某个List对象
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static List<T> Get()
|
||
{
|
||
return ListStack.Count == 0 ? new List<T>(8) : ListStack.Pop();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 入栈:将List对象添加到栈中
|
||
/// </summary>
|
||
/// <param name="toRelease"></param>
|
||
public static void Release(List<T> 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<T>(this List<T> self)
|
||
{
|
||
ListPool<T>.Release(self);
|
||
}
|
||
}
|
||
} |