Detailed Explanation of C Event Instance

  • 2021-12-21 04:47:40
  • OfStack

C # Event Instance Explanation

C # and JAVA have many similarities, The design ideas are similar, Grammar and its similarities, They all inherit the object-oriented design idea, Inspired by C + + and taking its essence to discard its "dross (2 words with reservations)", Intermediate language, interpretation and execution, compilation once, execution everywhere, being born into a wealthy family can be regarded as a cousin relationship, and each has a huge fan. On the road of development, the two languages catch up with each other, learn from each other, learn from each other and penetrate each other. As for whose IDE is stronger, it is really impossible to judge for a person who has used the vim editor for 210 years, and it is so powerful that people are full of cattle ~

Event (event) combined with agent (delegate) in C # language realizes the notification mechanism when the object state changes, I always feel that this treatment is a bit too complicated, But since people must have their own reasons for designing this way, Believe and learn from others without rushing to criticize and deny, so that you can make faster progress. Personally, I think this kind of processing probably comes from the unique mechanism of Windows system to respond quickly to each control event set. This may also be the difference in the focus of front-end and back-end development. Of course, the core of front-end human-computer interaction is events with uncertain time attributes and states, and the occurrence of each event triggered by back-end is basically predefined and constructed by process, so try to understand and interpret the events of C # under 1.

Step 1: Declare a proxy, which can be either system or custom.

 public delegate void MyDelegate();                   //  Declare a proxy without parameters and return value 

  public delegate bool MyDelegate(int k, int v);         //  Declare a proxy with reference and return value 

Step 2: Create an object containing the proxy event, and call the proxy in the object to implement the handling of the event.

  public class MyArrayList : ArrayList


    public event MyDelegate MyChanged;           //  Declare proxy events 

    public override void Add(object o)                //  Override parent class method 


       base.Add(o);                              //  Call the parent class method 

      OnChanged();                             //  Call event function 


    protected virtual void OnChanged()


      if (null != MyChanged) MyChanged();        //  Agent triggering event 



Step 3: Create a class, bind events and proxies to 1, a, class construction with proxy event object as input parameter, b, "+=" operator to realize binding, c, pass in class member function in proxies.

 public class MyEvent


    private MyArrayList list;

    public MyEvent(MyArrayList l)


      list = l;

      list.MyChanged += new MyDelegate(ListChanged);    //  Binding event 


    private void ListChanged()                           //  Bound event 


      System.Console.WriteLine("ListChanged ...");



Step 4: Create an instance of the class containing events and create an instance of the class containing methods.

  public class MyTest


    public static void Main()


      MyEvent me = new MyEvent(new MyArrarList());




After careful study, it is found that the proxy is equivalent to the function pointer in C\ C + +, but it is more powerful and safer to use. When the proxy instance is created, the proxy will pass the parameters passed to it to the bound method, and the proxy can carry more methods through the "+=" operator. The following is a comparison of the usage of C\ C + + function pointer.

  char* (*pFun)(char*) = NULL;

  pFun = GtCodeUtf8ToGB2312;

  char* pszData = (*pFun)(" Computer ");


  char* GtCodeUtf8ToGB2312(char* pszUtf8)


    char* pszGB2312 = NULL;


    return pszGB2312;


Give two more examples of typical function pointers in C language:

Example 1: int pthread_create (pthread_t * thread, const pthread_attr_t * attr, void * (*start_routine) (void *), void * arg); The third parameter is a function address, pointing to the core handler of the created thread.

Example 2: void *bsearch (const void * key, const void * base, size_t nmemb, size_t size, int (* compar) (const void *, const void *)); The last parameter is also a function address, pointing to the comparison calculation function between two elements.

Thank you for reading, hope to help everyone, thank you for your support to this site!

Related articles: