Deep parsing of events in C # programming

Source: Internet
Author: User
Tags modifier modifiers

An event is a member that enables an object or class to provide a bulletin. Users can add executable code to an event by providing an event handle. Events are declared using an event declaration:

An event declaration can be either an event domain declaration or an event property declaration. In each case, the declaration can consist of a collection of properties, a new modifier, a valid combination of four access modifiers, and a static modifier.

The type of an event declaration must be a representative type, and that representative type must be at least as accessible as the event itself.

An event field declaration corresponds to a domain declaration that declares one or more representative type fields. ReadOnly modifiers are not allowed in an event domain declaration.

An event property declaration corresponds to declaring a property declaration that represents a Type property. In addition to the event property declarations that contain both get and set accessors, member names and accessor declarations are the same for those property declarations and do not allow the virtual, override, and abstract modifiers to be included.

In the program text of a class or struct that contains an event member declaration, an event member is associated with a private field or property that represents a type, and that member can be used in any context that allows the use of a domain or property.

If the program text of a class or struct contains an event member declaration, this event member can only be used as the right operand of the + = and-= operator (§). These operators are used to attach or remove event handlers for event members, and the access operator of this event member controls the context in which the operation is executed.

Because + + and-= are the only operations that can be used on events outside the type that declares the event member, the external code can add or remove handles for an event, but you cannot obtain or modify the values of the underlying event field or event properties in any other way.

In the example

public delegate void EventHandler(object sender, Event e);
public class Button: Control
{
 public event EventHandler Click;
 protected void OnClick(Event e) {
  if (Click != null) Click(this, e);
 }
 public void Reset() {
  Click = null;
 }
}

There is no limit to the Click event field in the Button class. As an example of a demo, this field can be examined, modified, and used in representative invocation expressions. The OnClick method in the class button "causes" the Click event. The concept that causes an event is exactly the same as the invocation represented by the event member-therefore, there is no special language construct for causing the event. Note that the representative's call is made by checking that the guarantee represents a Non-empty post.

Outside the declaration of the Class button, member Click can only be used on the right-hand side of the + = and-= operators, as follows

b.Click += new EventHandler(...);

It attaches a delegate to the invocation list of the event click, and

b.Click -= new EventHandler(...);

It removes a representation from the invocation list of the Click event.

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.