Pau*_*tos 5 .net c# events scope
假设我们有以下设置:
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!!
}
}
Run Code Online (Sandbox Code Playgroud)
SomeEvent在"问题点"之后提出事件会发生什么?
它将调用处理对象的方法.这就是取消订阅很重要的原因.它甚至可能导致内存泄漏.