Rating C # event handling

Source: Internet
Author: User

I learned a little about C # a few days ago. For the sake of "msdn Development selection", I can't tell you what to do. I didn't read any language books. That is to say, I will refer to Jefferey Richter's "applied. NET Framework" (translated by Li Jianzhong. It is annoying to see the part of the event. The delegate and event of C # are only superficial, and the Implementation below is very cumbersome. A long linked list contains closure pointers. The type is safe enough, and the space and time efficiency are not small. It is uncomfortable for me to climb from the C language.

The event processing mechanism is one of the biggest weaknesses of C ++ and its successors (Java and C. Essentially, because of the current year's selection of C ++, the common message passing between objects is solved by calling the member function. The call speed of member functions is fast enough, but the tight coupling between message senders and recipients is caused. In most cases, this is not a big deal. In terms of event processing, loose coupling is required in practice, so the issue of C ++ is exposed. No. All the GUI frameworks written in C ++ are making a big fuss about the event. Relatively stupid global ing tables such as MFC, smart ones such as signal/slot of QT, one exception is VCL, Borland implements closure pointer, the caller context and pointer are bound together and sent to callee. Java is quite satisfactory. The event processing mechanism is implemented in the most oo way, but the scalability is poor. C #'s delegate is actually a new version of closure pointer in VCL. In short, the Eight Immortals pass through the sea and show their respective capabilities. However, I think C ++ has been working hard in vain. As long as this idea persists, event processing cannot be very elegant. C # It can make the whole thing look pretty on the surface, but it's still messy.

Personally, I still prefer C's solution: to define a protocol, I prepare the event data in one place, and then ignore it. the receiver of the event retrieves the data and parses the data by himself, determine how to handle the problem.

This type of receiver is called active object in object classification. Over the past two years, in the middle of embedded system programming, we have increasingly discovered the advantages of active objects. I personally think this problem should also be considered in the mainstream programming field.

The problem with the active object may be that the type is insecure, but in fact the type security is not that important, and there is no need to hold it together. In the original C language:
INT (* compare )();
The declared pointer can point to any function that returns an integer, regardless of its parameter list. In C ++, this pointer can only point to a no-argument function that returns an integer, greatly reducing the flexibility. It's no wonder that open source people prefer C. Although security and flexibility are equally important, real hackers can use their own technologies to ensure security, but cannot accept the lack of flexibility.

Far away.

In short, the more you read, the more you think C is really a great language, probably only Fortran and lisp are at the same level with it.

Additionally, the quality of Li Jianzhong's translation is extremely high, which makes us amazed.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.