Naming Conventions for events, delegates, and CLR events

Source: Internet
Author: User

In event communication, the event sender class does not know which object or method will receive (process) the event it raises .. Net Framework defines a type (delegate) that provides mediation between events and event handlers. Technically, a delegate is a class that can save reference to a method. Unlike other classes, the delegate class has a signature and can only reference methods that match the signature. Although delegation has many other purposes, only the delegate event processing function is discussed here. The following example shows the event Delegate statement.

public delegate void AlarmEventHandler(object sender, AlarmEventArgs e);

According to the Conventions, the event Delegate in. NET Framework has two parameters: the event source (represented by the sender parameter) and the event data (represented by the E parameter ).

You should call the protected oneventname method from the derived class (if this method is available) to raise the event. The oneventname method calls the delegate and transmits any event-specific data to trigger the event. The event Delegate method can perform event operations or process event-specific data.

The protected oneventname method also allows the derived class to override the event without attaching a delegate to it. The derived class must always call the oneventname method of the base class to ensure that the registered delegate receives the event. However, in a sealed or notinheritable class, you can directly trigger an event instead of indirectly triggering an event in the oneventname method.

Private alarmeventhandler m_alarmevent;

Public alarmeventhandler alarmevent

{

Add {This. m_alarmevent + = value ;}

Remove {This. m_alarmevent;-= value ;}

}

Public void onalarmevent

{

If (this. m_alarmevent! = NULL ){

This. (This. m_alarmevent (null, null)

}

}

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.