Negative tive C # item35: overwrite is better than event Processor

Source: Internet
Author: User

Many classes in. Net provide two different ways to handle events from the system. On the one hand, we can use the event processor, and on the other hand, we can override the Virtual Methods of the base class. These two methods have different purposes. In a derived class, we should rewrite the virtual method to process system events.

For the above two methods, let's look at the following code.

Code

  1 public class MyForm : Form
2 {
3
4 // Other code elided.
5
6 protected override void OnMouseDown(
7 MouseEventArgs e )
8 {
9 try {
10 HandleMouseDown( e );
11 } catch ( Exception e1 )
12 {
13 // add specific error handling here.
14 }
15 // *almost always* call base class to let
16 // other event handlers process message.
17 // Users of your class expect it.
18 base.OnMouseDown( e );
19 }
20 }
21
22
23 public class MyForm : Form
24 {
25
26 // Other code elided.
27
28 public MyForm( )
29 {
30 this.MouseDown += new
31 MouseEventHandler( this.MouseDownHandler );
32 }
33
34 private void MouseDownHandler( object sender,
35 MouseEventArgs e )
36 {
37 try {
38 HandleMouseDown( e );
39 } catch ( Exception e1 )
40 {
41 // add specific error handling here.
42 }
43 }
44 }

The above Code uses two methods to implement window click events. If the two methods are used in a form, the virtual method of rewriting is first executed before the event processor is executed.

We recommend that you override the parent class virtual method to handle events for the following reasons:

  1. We know that events are implemented based on delegation, and both events support multicast. In other words, we can provide a series of methods for a system event, A chain is formed, and the methods in this chain are called in sequence. However, if an exception occurs when a method in the middle is called, the subsequent methods will not be called. In this case, it is very likely that an exception is thrown by a method during event processing, which will cause the methods in the event chain not to be fully called.
  2. The rewrite virtual method is better than the event processor in terms of performance. This is caused by the event processing mechanism of the form, because the form may trigger many events, therefore, a complex collection mechanism is generally used to store event processors and map corresponding processors to specific events, it takes a long CPU time to process the event, because it must check whether the event is associated with an event processor. If yes, it must iterate the entire request list, each method in the event request list must be called. Checking whether an event processor exists and iteration of each processor at runtime takes more execution time than calling a virtual function.

Therefore, when processing events in a derived class, we recommend that you override the virtual method to process system events. This includes: 1) events of the outermost control of an application, such as a form; 2) User-Defined controls. For example, when we create a button, we need to re-paint the appearance of the button and customize the button's click event. In this case, we should rewrite the virtual method.

In the following cases, we should use the event processor to handle system events:

  1. Some widget events in the screen. For example, if a button control exists in the screen and a button has a click event, it is not appropriate to rewrite the virtual method, because the Click Event Logic is written on form.
  2. Because the event responds at runtime, if our event processing method is not fixed, we should adopt the event processing method. In different situations, bind different processing methods for the event.

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.