zoukankan      html  css  js  c++  java
  • delegate vs event

    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.

    1. publicclassAnimal
    2. {
    3. publicActionRun{get;set;}
    4. publicvoidRaiseEvent()
    5. {
    6. if(Run!=null)
    7. {
    8. Run();
    9. }
    10. }
    11. }

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

    1. Animal animal=newAnimal();
    2. animal.Run+=()=>Console.WriteLine("I'm running");
    3. animal.Run+=()=>Console.WriteLine("I'm still running");
    4. animal.RaiseEvent();

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

    1. animal.Run+=()=>Console.WriteLine("I'm running");
    2. animal.Run+=()=>Console.WriteLine("I'm still running");
    3. 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:

    1. if(animal.Run!=null)
    2. {
    3. animal.Run();
    4. }

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

    Example with Event

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

    1. publicclassArgsSpecial:EventArgs
    2. {
    3. publicArgsSpecial(string val)
    4. {
    5. Operation=val;
    6. }
    7. publicstringOperation{get;set;}
    8. }
    9. publicclassAnimal
    10. {
    11. // Empty delegate. In this way you are sure that value is always != null
    12. // because no one outside of the class can change it.
    13. publiceventEventHandler<ArgsSpecial>Run=delegate{};
    14. publicvoidRaiseEvent()
    15. {
    16. Run(this,newArgsSpecial("Run faster"));
    17. }
    18. }

    to call events

    1. Animal animal=newAnimal();
    2. animal.Run+=(sender, e)=>Console.WriteLine("I'm running. My value is {0}", e.Operation);
    3. 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:

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

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

  • 相关阅读:
    PHP 关于字符串操作的练习
    PHP 字符串操作
    JavaScript DOM级事件
    JavaScript 会闪烁的文字
    JavaScript 当输入框获得焦点:如果输入框值为空,提示输入你的姓名,当输入框失去焦点,如果输入框值为空,提示用户名不能为空,边框颜色变为红色,如果输入框值不为0,那么不提示边框默认颜色
    Js 特效之鼠标点击出现小心心特效
    JavaScript BOM对象 DOM对象
    JavaScript 当用户在弹出的输入框中输入手机号码后,将手机号码的前7位转化为*号
    JavaScript 编写代码对用户输入内容的输入框进行排查,看有没有敏感字“草”字
    第一次c++作业小结
  • 原文地址:https://www.cnblogs.com/egoechog/p/6391074.html
Copyright © 2011-2022 走看看