nab*_*bil -1 c# events delegates
请给我一个简单的例子,为什么我们需要它,如果我们没有在带有委托的例子中使用它会发生什么.谢谢
当然:
// With events...
public class Button
{
    public event EventHandler Click;
}
public void Client
{
    public void Method(Button button)
    {
        // This is all I can do...
        button.Click += SomeHandler;
    }
}
// With plain delegate fields or properties...
public class Button
{
    public EventHandler Click { get; set; }
}
public void Client
{
    public void Method(Button button)
    {
        // Who cares if someone else is already subscribed...
        button.Click = SomeHandler;
        // And let's just raise the event ourselves...
        button.Click(button, EventArgs.Empty);
    }
}
换句话说,对于事件,您已经控制了发布/子模式的实现 - 单独的订阅者不能相互干扰(除非可能通过在其处理程序中抛出异常)并且只有发布者可以"发布"(调用处理程序).
| 归档时间: | 
 | 
| 查看次数: | 129 次 | 
| 最近记录: |