Event Delegate:
A delegate is a type of reference method.
Once a method is assigned to the Delegate, the delegate has the same behavior as the delegate.
The use of delegate methods can have parameters and return values like any other method.
A delegate can be seen as an abstraction of a function and a class of a function. A delegated instance represents a specific function.
A single delegate can carry multiple methods, and all methods are aroused at one time.
The methods carried by the delegate object do not need to belong to the same class.
The methods carried by the delegate object must have the same prototype and form, that is, the same parameter-type table and return type.
Demo:
// Declare a delegate called eventhandler (Event ProcessingProgram), No parameter, no return value
Delegate void eventhandler ();
Classboss: Subject
{
// Declare an event update. The type is the delegate eventhandler.
// Declare an eventhandler (event handler) delegate event, called update
Public event eventhandler update;
Public void Policy ()
{
Update ();
}
Pragram:
// The closestockmarket method registers to update by instantiating the delegate
// + = Add_update ()
Boss. Update + = neweventhandler (tongshi1.closestockmarket );
Boss. Y ();
A method in the tongshi1 object:
Public void closestockmarket ()
{
Console. writeline ("{0} {1} Close the stock. ", Sub. subjectstate, name );
}
Add parameters:
Publicdelegate void cateventhandler (Object sender, cateventargs ARGs );
Publicclass cateventargs:Eventargs
{Public string name {Get; Set ;}}