zoukankan      html  css  js  c++  java
  • Delegate and Event

    Delegate is very easy to understand. So no need to waste words here.

    I would like to talk more about Event in C#. 

    1. [Paste from unleashed C#]An event is a delegate with special features in the areas of type membership, limitations on invocation, and assignment. By having events as first-class type members, along with fields, methods, and properties, C# becomes a more component-oriented language. This means that other objects can listen for changes in your object and receive notifications via event invocations.
    Events offer additional protections that you don’t have with raw delegates, one being that the event can be invoked only inside of its containing type. Another protection is preventing
    direct assignment to the event. Because of these protections, events are preferred over delegates for holding delegate instances and invoking delegates.

    2. Firing Events
    When events are invoked, they are also said to be fired. Events are fired from within the class that defines them. Outside of their class, they can be used only on the left side of a
    combine or remove operation. See below example:

    class myTestClass

    {

      public event MyDelegate myEvent;

      public void FireMyEvent()

       {

        myEvent(); // This is the only way to call myEvent. If calling myEvent outside, compiler will tell you error.

       }

    }

    3. Modifying Event Add/Remove Methods

    public event MenuHandler MenuSelection
    {
    add
    {
    mh += value; // private MyDelegate mh;
    numberOfEvents++;
    }
    remove
    {
    mh -= value;
    numberOfEvents—;
    }

  • 相关阅读:
    a标签上window.location.href无法跳转
    Directive指令的scope配置项使用说明
    Echarts 里面获取纵坐标刻度的间距
    使用 Supervsior 守护进程
    linux 下的快捷键操作
    前端必须掌握的 nginx 技能(4)
    在 vue 中用 transition 实现轮播效果
    前端必须掌握的 nginx 技能(3)
    前端必须掌握的 nginx 技能(2)
    前端必须掌握的 nginx 技能(1)
  • 原文地址:https://www.cnblogs.com/taoxu0903/p/1705814.html
Copyright © 2011-2022 走看看