C # events and indexers

Source: Internet
Author: User

1 events

An event is a way for a class to provide notification in the event of something that it concerns. For example, a class that encapsulates a user interface control can define an event that occurs when the user clicks the control. The control class doesn't care what happens when the button is clicked, but it needs to tell the derived class that the Click event has occurred. The derived class can then choose how to respond.

C # is a fully object-oriented component program, so it allows you to write custom events. In fact, the event is a callback (callback) function pointer, which is achieved through delegate in C #.

In the. NET event model, the main roles are event publishers and event subscribers. The publisher of an event is the object that triggers the event, and the Subscriber to the event refers to the person who registered to be notified when an event occurred.

When events related to an object occur, classes and structs use events to notify the user of this object. This notification is called "Raising an event." The object that raises the event is called the source or sender of the event. Objects raise events For many reasons: responding to changes to object data, long-running process completion, or service outages. For example, an object raises an event if a network connection is lost when using a network resource. Objects that represent user interface elements typically raise events to respond to user actions, such as button clicks or menu selections.

To use an event in a program, you must first declare the event. Here's a declaration of an event with the following code:

public delegate void Mreventdelegate (object sender, System.EventArgs e);

In the signature of events in the. NET Framework, the first argument is the object that refers to the source of the event, and the second parameter is a class that transmits data related to the event. However, this form is not enforced in the C # language, and other aspects can be the same as any valid delegate signature, as long as the event signature returns void.

Adding an event to a class requires using the event keyword and providing the delegate type and event name. For example:

public class Eventsend
{
public event Mreventdelegate TestEvent;
private void Raisetestevent () {/* ... */}
}

Events can be marked as public, private, protected, internal, or protectedinternal. These access modifiers define how users of the class access events.

To raise an event, the class can invoke the delegate and pass all parameters related to the event. The delegate then invokes all the handlers that have been added to the event. If the event does not have any handlers, the event is empty. Therefore, before the event is raised, the event source should ensure that the event is not empty to avoid nullreferenceexception. To avoid contention conditions (the last handler is removed between the empty check and the event call), the event source should also create a copy of the event before performing an empty check and raising an event. The following code implements the trigger Mreventdelegate event:

private void Raisetestevent ()
{
Mreventdelegate temp = testevent;
if (temp!= null)
{
Temp (this, new System.EventArgs ());
}
}

Each event can be assigned more than one handler to receive the event. In this case, the event automatically invokes each sink, and the event is raised only once, regardless of the number of sinks. The class to receive an event can create a method to receive the event, and then add a delegate to the class event itself. This process is called "subscribing to events."

First, the receiving class must have a method that has the same signature as the event itself, such as a delegate signature. The method, called an event handler, can then take the appropriate action to respond to the event. For example:

public class Eventreceiver
{
public void Receivetestevent (object sender, System.EventArgs e)
{
System.Console.Write ("Event received from");
System.Console.WriteLine (sender. ToString ());
}
}

Related Article

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.