国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

delegate vs event

2019-11-11 04:50:15
字體:
供稿:網(wǎng)友

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.


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 和田市| 光泽县| 雷山县| 阜城县| 政和县| 老河口市| 仁怀市| 榆中县| 平邑县| 武定县| 奉节县| 天峨县| 泸西县| 汶川县| 噶尔县| 永川市| 清苑县| 伊吾县| 西峡县| 黄山市| 舟曲县| 江油市| 历史| 赤壁市| 黄平县| 陇川县| 房产| 东兰县| 宁海县| 黄陵县| 洪洞县| 西乌珠穆沁旗| 资兴市| 宁阳县| 泌阳县| 上杭县| 饶河县| 广西| 五大连池市| 麦盖提县| 普兰店市|