Ver.0.3.0.33
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user