与事件处理程序会发生什么,当一个对象超出范围? [英] What happens with event handlers when an object goes out of scope?

查看:120
本文介绍了与事件处理程序会发生什么,当一个对象超出范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我们有如下设置:

Let's say we have the following setup:

public class ClassA
{
   public event EventHandler SomeEvent;
}

public class ClassB : IDisposable
{
   public void SomeMethod(ClassA value)
   {
      value.SomeEvent += (s, e) => { DoSomething(); };
   }

   void DoSomething() { }

   void Dispose() { }
}

public static class Program
{
   static void Main()
   {
      var a = new ClassA();

      using (var b = new ClassB())
         b.SomeMethod(a);

      // POINT OF QUESTION!!
   }
}

当事件 SomeEvent 点问题后,提出了会发生什么情况?

What happens when the event SomeEvent is raised after the "POINT OF QUESTION"?

推荐答案

它会调用释放对象的方法。这就是为什么它是重要退订。它甚至可以导致内存泄漏。

It will call method of disposed object. That's why it is important to unsubscribe. It even can lead to memory leaks.

这篇关于与事件处理程序会发生什么,当一个对象超出范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆