Eventhandler generic delegation
// This example demonstrates the eventhandler <t> delegate. Using System; Using System. Collections. Generic; //--------------------------------------------------------- Public Class Myeventargs: eventargs { Private String MSG; Public Myeventargs ( String Messagedata) {MSG = messagedata ;} Public String Message { Get { Return MSG ;} Set {MSG = value ;}}} //--------------------------------------------------------- Public Class Hasevent { // Declare an event of delegate type eventhandler // Myeventargs. Public Event Eventhandler <myeventargs> sampleevent; Public Void Demoevent ( String Val ){ // Copy to a temporary variable to be thread-safe. Eventhandler <myeventargs> temp = sampleevent; If (Temp! = Null ) Temp ( This , New Myeventargs (VAL ));}} //--------------------------------------------------------- Public Class Sample { Public Static Void Main () {hasevent He = New Hasevent (); he. sampleevent + = New Eventhandler <myeventargs> (sampleeventhandler); he. demoevent ( "Hey there, Bruce! " ); He. demoevent ( "How are you today? " ); He. demoevent ( "I'm pretty good ." ); He. demoevent ( "Thanks for asking! " );} Private Static Void Sampleeventhandler ( Object SRC, myeventargs MEA) {console. writeline (MEA. Message );}} //--------------------------------------------------------- /* This example produces the following results: Hey there, Bruce! How are you today? I'm pretty good. Thanks for asking! */
. NET Framework 2.0 Other Versions 11 (16 in total) is helpful for the evaluation of this Article-Comment on this topic
Note: This delegate is added in. NET Framework 2.0.
Indicates the method to process the event. The generic type parameter specifies the type of event data generated by the event.
Namespace:System
ProgramSet:Mscorlib (in mscorlib. dll)
Syntax
C # C ++ VB
[Serializableattribute]Public Delegate VoidEventhandler <teventargs> (Object sender, teventargs E)WhereTeventargs: eventargs
J #
J # supports generic types and methods, but does not support new declarations.
JScript
JScript supports generic types and methods.
Parameters
- Sender
-
Event source.
- E
-
Eventargs that contains event data.
Remarks
The event model in. NET Framework is based on an event delegate that connects the event to the event handler. Two elements are required for triggering an event:
A delegate is a type that defines the signature, that is, the return value type and parameter list type of the method. You can use the delegate type to declare a variable. This variable can reference all methods that are the same as the delegate signature.
The standard signature entrusted by the event handler defines a method without return values. The first parameter type is object, which references the instance that triggers the event, and the second parameter is fromEventargsType, which stores event data. If the event does not generate event data, the second parameter isEventargs. Otherwise, the second parameter isEventargsA derived custom type that provides all the fields or attributes required to save event data.
EventhandlerIs a predefined delegate, indicating the event processing method. It has nothing to do with whether the event generates event data. If the event does not generate event data, useEventargsReplace the generic type parameter. Otherwise, you can use the Custom Event data type to replace the generic type parameter.
UseEventhandlerThe advantage is that if an event generates event data, you do not need to write your own custom delegate.Code. In addition, the. NET Framework supports only one implementation.EventhandlerThis is irrelevant to the event data type that replaces the generic type parameter.
To associate an event with the event handling method, add a delegated instance to the event. Unless the delegate is removed, the event handler is called whenever this event occurs.
For more information about event handler delegation, see events and delegation.
Example
The following code example declares the event data andEventhandlerGeneric delegation, and demonstrate how to raise this event.
C # VB
// This example demonstrates the eventhandler <t> delegate. Using System; Using System. Collections. Generic; //--------------------------------------------------------- Public Class Myeventargs: eventargs { Private String MSG; Public Myeventargs ( String Messagedata) {MSG = messagedata ;} Public String Message { Get { Return MSG ;} Set {MSG = value ;}}} //--------------------------------------------------------- Public Class Hasevent { // Declare an event of delegate type eventhandler // Myeventargs. Public Event Eventhandler <myeventargs> sampleevent; Public Void Demoevent ( String Val ){ // Copy to a temporary variable to be thread-safe. Eventhandler <myeventargs> temp = sampleevent; If (Temp! = Null ) Temp ( This , New Myeventargs (VAL ));}} //--------------------------------------------------------- Public Class Sample { Public Static Void Main () {hasevent He = New Hasevent (); he. sampleevent + = New Eventhandler <myeventargs> (sampleeventhandler); he. demoevent ( "Hey there, Bruce! " ); He. demoevent ( "How are you today? " ); He. demoevent ( "I'm pretty good ." ); He. demoevent ( "Thanks for asking! " );}Private Static Void Sampleeventhandler ( Object SRC, myeventargs MEA) {console. writeline (MEA. Message );}} //--------------------------------------------------------- /* This example produces the following results: Hey there, Bruce! How are you today? I'm pretty good. Thanks for asking! */
Platform
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for smartphone, Windows Server 2003, Windows XP Media Center edition, Windows XP Professional x64 Edition windows XP SP2, Windows XP Starter Edition
. NET Framework does not support all versions of each platform. For a list of supported versions, see system requirements.
Version Information
. NET Framework
Supported by: 2.0
. NET Compact framework
Supported by: 2.0