CLR notes: 10. Events

Source: Internet
Author: User
Tags instance method

Events are also methods.

Defining an event member means that the type has three capabilities:

* Static method/instance method of type can subscribe to type event

* Static method/instance method of type can unregister type event

* methods to notify subscribed events when events occur

. The NET2.0 event is still based on Win32, but it is implemented using the observer pattern, and is built on the delegate mechanism.

The design steps for the event are as follows (basically the observer implementation steps):

10.1 Design an external type of event

1. Define eventargs or subclasses to store additional information:

Defines a class that inherits from EventArgs, ends with EventArgs, and contains a set of private fields and corresponding read-only public properties.

public class Newmaileventargs:eventargs

{

private string from;


public string from

{to

}

}

}

Here, the EventArgs base class looks like this in FCL:

 [Serializable]

[ComVisible (True)]

public class EventArgs

{

//Summary:

//Indicates an event with no event data.        Public

static readonly EventArgs Empty;


public EventArgs ();

}

Most events do not have data attached, so you do not have to define any private fields and properties and use the EventArgs base class directly as parameters.

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.