Non-Standard Event Declaration

suggest change

Events can be of any delegate type, not just EventHandler and EventHandler<T>. For example:

//Declaring an event
public event Action<Param1Type, Param2Type, ...> EventName;

This is used similarly to standard EventHandler events:

//Adding a named event handler
public void HandlerName(Param1Type parameter1, Param2Type parameter2, ...) {
    /* Handler logic */
}
EventName += HandlerName;

//Adding an anonymous event handler
EventName += (parameter1, parameter2, ...) => { /* Handler Logic */ };

//Invoking the event
EventName(parameter1, parameter2, ...);

It is possible to declare multiple events of the same type in a single statement, similar to with fields and local variables (though this may often be a bad idea):

public event EventHandler Event1, Event2, Event3;

This declares three separate events (Event1, Event2, and Event3) all of type EventHandler.

Note: Although some compilers may accept this syntax in interfaces as well as classes, the C# specification (v5.0 ยง13.2.3) provides grammar for interfaces that does not allow it, so using this in interfaces may be unreliable with different compilers.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Events:
* Events
* Non-Standard Event Declaration

Table Of Contents
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
46 Methods
88 Events
92 Structs
104 Indexer
106 Stream
107 Timers
109 Threading
127 Caching
135 Pointers
147 C# Script