Valid C # item 35: prefer overrides to event handlers

Source: Internet
Author: User

Valid C # item 35: prefer overrides to event handlers

The classes in many. Net class libraries provide two different methods for processing event handles. You can add an event to it or override the event abstraction method of its base class. Why do we provide two different methods for the same thing? This is for different situations. When implementing a derived class, a better choice is to override the abstract methods in the base class.

Suppose we are writing a Windows application. This program needs to respond to the event of mouse pressing. In the custom form class, we can choose to override the onmousedown () event:

Public class myform: Form
{
Protected override void onmousedown (mouseeventargs E)
{
Try
{
Handlemousedown (E );
}
Catch
{
// Error
}
Base. onmousedown (E );
}
}

Or add the event handle:

Public class myform: Form
{
Public myform ()
{
This. mousedown + = new mouseeventhandler (myform_mousedown );
}
 
Void myform_mousedown (Object sender, mouseeventargs E)
{
Try
{
Handlemousedown (E );
}
Catch
{
// Error
}
}
}

The first method is recommended here. Once the event handle throws an exception, no other event handles will be called. This avoids some problems caused by the continued calling of error codes. By Rewriting the protected virtual method, our handle can be called first. The virtual functions in the base class are responsible for calling other related handles. This means that if you need to call the event handle (generally required), you need to call the virtual functions of the base class. In some special circumstances, we need to replace the default behavior of the base class, and may not need to call any original event handle. Although we cannot ensure that all event handles are executed, it may throw an exception, but we can ensure that the behavior of the derived class is correct.

Using override is much more efficient than adding an event handle. In item 22, we show how the system. Windows. Forms. control class stores the handle time and maps it to each event. This kind of event mechanism will cause more consumption due to checking the event handle. Each method in the event handle list needs to be executed. Compared with the rewrite virtual method, event processing consumes more time.

If this is not enough, we can look back at the code at the beginning. Which one is clearer? To override the virtual method, you only need to maintain a function to check and modify it. The event mechanism requires two maintenance points: the event handle function and the event binding code. Any of these points may cause overall functional failure. A function is obviously easier.

These are the reasons for rewriting and not event handling. However,. Net designers also have a reason to provide events. They will not do useless work. This type of rewriting applies to derived classes. In addition, we must use the event mechanism. For example, we often need to add a button click event for the form. This event is triggered by a button and processed in form. Of course, we can create a custom button and then rewrite the virtual method in it, but this is too cumbersome. To handle an event, we need to create a custom button class, it's all about finding trouble for yourself. However, using the event mechanism is very simple. This is also a reason for. NET Framework designers to design the event mechanism.

Another reason is that the event is bound at runtime. We can handle events more flexibly and bind different events to them at runtime. Suppose we are writing a drawing program. clicking the mouse may start with the draw line or select an object. We can switch these events when switching the mode. In addition, we can add multiple event handles for the same event.

When creating a derived class, we should use the method of rewriting the virtual function to process the event. This makes maintenance easier and more efficient. In other cases, the event handle should be used.

Translated from Objective C #: 50 specific ways to improve your C # by Bill Wagner

Back to directory

 

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.