class Expensive
{
static Stack pool = new Stack();
public static Expensive GetObjectFormPool()
{
return (Expensive)pool.Pop();
}
public static void ShutDownPool()
{
pool = null;
}
public Expensive()
{
pool.Push(this);
Console.WriteLine("add a object");
}
~Expensive()
{
if (pool != null)
{
Console.WriteLine("clear the memory");
GC.ReRegisterForFinalize(this);
pool.Push(this);
}
}
public void PrintTest()
{
Console.WriteLine("Hello World!");
}
public static int GetCount()
{
if (pool != null)
{
return pool.Count;
}
return 0;
}
}
对象池的创建 c#学习
最新推荐文章于 2023-08-16 16:36:21 发布