这是一个随机问题。动态事件处理
我有一个叫做Monkey的对象,和一个叫做Banana的对象。香蕉揭露了Monkey对象订阅的一个叫做Ripens的事件。当Ripens事件被触发时,猴子调用它的Consume()函数,这反过来破坏了香蕉对象。
例子:
//And yes, I know this isnt real C# code. Just trying to get my point across and
//not necessarily be syntatically correct with this exmaple.
public class Banana
{
public event Ripens;
}
public class Monkey
{
public Monkey()
{
List<Banana> tree = new List<Banana>();
for (int i = 0; i < 8; i ++)
{
tree.Add(new Banana());
tree[i].Ripens += this.Consume;
}
}
public void Consume(Banana b)
{
tree.Remove(b);
b.Destroy();
}
}
所以我的问题是,然后:请问猴子流血内存,以便在没有从事件第一退订摧毁每一根香蕉。或者猴子的事件处理程序与香蕉一起销毁?
http://stackoverflow.com/questions/298261/do-event-handlers-stop-garbage-collection-from-occuring的副本。答案是否定的,不管是否有订阅,都会收集香蕉。 – Andrei 2011-06-12 23:20:14