麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

delegate vs event

2019-11-11 04:50:15
字體:
來源:轉載
供稿:網友

What are the differences between delegate and an event

An event declaration adds a layer of abstraction and PRotection on the delegate instance. This protection prevents clients of the delegate from resetting the delegate and its invocation list and only allows adding or removing targets from the invocation list. To understand the differences you can look at 2 examples below:

Example with Delegate

First let's try to implement an "event trigger" by using "delegate" instead of "event". in this case, the example delegate Run is an Action - that is a kind of delegate that doesn't return a value.

public class Animal{public Action Run {get; set;}public void RaiseEvent(){if (Run != null){Run();}}}

To use the delegate, you should do something like this:

Animal animal= new Animal();animal.Run += () => Console.WriteLine("I'm running");animal.Run += () => Console.WriteLine("I'm still running") ;animal.RaiseEvent();

This code works well but you could have some weak spots: For example, if I write this:

animal.Run += () => Console.WriteLine("I'm running");animal.Run += () => Console.WriteLine("I'm still running");animal.Run = () => Console.WriteLine("I'm sleeping") ;

with the last line of code, I have overridden the previous behaviors just with one missing + (I have used = instead of +=)

Another weak spot is that every class which uses your Animal class can raise the Run event without calling the public RaiseEvent function, but with code snippet like:

if (animal.Run != null){   animal.Run();}

To avoid these weak spots you can use events in c#.

Example with Event

Your "event version" of the Animal class will looks like:

public class ArgsSpecial : EventArgs{    public ArgsSpecial (string val)    {        Operation=val;    }    public string Operation {get; set;}} public class Animal{    // Empty delegate. In this way you are sure that value is always != null     // because no one outside of the class can change it.    public event EventHandler<ArgsSpecial> Run = delegate{};    public void RaiseEvent()    {           Run(this, new ArgsSpecial("Run faster"));    }}

to call events

 Animal animal= new Animal(); animal.Run += (sender, e) => Console.WriteLine("I'm running. My value is {0}", e.Operation); animal.RaiseEvent();

Differences:

1. You aren't using a public property but a public field. Using events, the compiler protects your fields from unwanted access 2. Event can't be assigned directly. In this case, it is impossible to override the previous behaviors by using = instead of +=. 3. No one outside of your class can raise the event. Even the Run event is public, a compiler error will occur if someone tries to raise the event with code snippet below:

// Error: the event 'delegateEvent.Animal.Run' can only appear on the left hand side of += or -= // (except when used from within the type 'delegateEvent.Animal')animal.Run(animal, new ArgsSpecial("Run slower"));

4. Event can be included in an interface declaration, whereas a delegate field cannot.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 亚洲午夜精选 | 视频一区二区在线观看 | 欧美成人理论片乱 | 国产精品久久久久久模特 | 久久精品com | 欧美a级在线免费观看 | 一级一片免费看 | 国产亚洲综合一区二区 | 精品国产精品久久 | 97视频| 麻豆一二区 | 亚洲一级片免费观看 | 成年人高清视频在线观看 | 黄色av免费网站 | 久草在线资源福利站 | 亚洲精品成人久久久 | 97黄色网| 黄色毛片观看 | 依依成人精品视频 | 国产羞羞视频在线观看 | 成人毛片免费播放 | 媚药按摩痉挛w中文字幕 | 亚州精品国产 | 欧美成视频在线观看 | 色播视频网站 | 久久精品一区二区三区不卡牛牛 | 日产精品久久久一区二区福利 | 成人午夜在线免费视频 | 毛片av网 | 日本高清一级片 | 国产精品成人亚洲一区二区 | 国产chinesehd精品91 | 日日天日日夜日日摸 | 国产免费传媒av片在线 | 爱福利视频网 | 全黄裸片武则天艳史 | h视频免费看 | 欧美自拍三区 | 黄色特级片黄色特级片 | 九九精品久久 | 国产一极毛片 |