10-05 Understanding C# Events

10-05 Understanding C# Events

理解C#事件

Delegates are fairly interesting constructs(柯1)(建造,构筑) in that they enable objects in memory to engage in a two-way conversation. 

委托是相当有趣的结构,这种结构能够使内存中的对象进行双向通信。

Exposing public delegate members breaks encapsulation, which not only can lead to code that is hard to maintain (and debug), but could also open your application to possible security risks!

暴露公共委托成员破坏了封装性,造成了不但导致代码很难去维护(调试),并且使你的程序可能的安全风险。

10-05-01 The C# event Keyword

As a shortcut, so you don’t have to build custom methods to add or remove methods to a delegate’s invocation list, C# provides the event keyword. When the compiler processes the event keyword, you are automatically provided with registration and unregistration methods, as well as any necessary member variables for your delegate types. 

10-05-02 Events Under the Hood

事件的底层结构

When the compiler processes the C# event keyword, it generates two hidden methods, one having an add_ prefix, the other having a remove_ prefix. 

 

 

posted @ 2015-02-05 10:51  海川0  阅读(78)  评论(0编辑  收藏  举报