1.1. Delegation
You can think of a delegate as an object that holds one or more methods. Will execute all his "hold" methods when executed.
You can see it. A type-safe, object-oriented C + + function pointer
Delegates, like classes, are a type of user customization. And the delegate holds one or more methods.
delegate void Mydel (int value); Declaring a delegate type
# declares a delegate that looks similar to a method declaration, but does not implement a block.
# Declare a delegate variable using the delegate type.
# Create a Delegate object, assign a value to the delegate variable. The new delegate object includes a reference to a method, with the signature (including ref,out) consistent with the return type. You can add methods
# You can invoke a delegate just as you would call a method. Each method contained will be executed. It can be an instance method or a static method.
# Call a delegate with a return value. The value returned by the last method is the return value of the delegate, and the other method return value is ignored
# when calling a delegate with a reference parameter, the parameter value may change. The parameter values are different when you execute each method
delegate void Printfunction ();
Class Program
{
}
1.2. Delegate use of anonymous methods
# declares the delegate yojgjf as an expression to initialize.
# adds an event to the right of an assignment statement for a delegate
# In addition to the array parameters, the argument list must match the delegate: number of parameters, type and position, modifier
# You can omit parentheses to simplify the argument list, but you must meet 2 criteria: The delegate parameter list does not contain out parameters;
# If the delegate declares a params parameter, then the parameter list of the anonymous method ignores the params keyword.
1.3.LAMBDA-expression
# allows us to omit the type parameter (implicit type). If there is only one implicit type argument, we can omit the parentheses;
# allow the body of an expression to be a statement block or an expression
# with ref, the out parameter must indicate the type (explicit type)
# If there are no arguments, you must use a set of empty parentheses
2.1 Publisher/Subscriber mode
The publishing class defines a series of events that other classes can "register" so that publishers can notify them when these events occur. These subscriber classes get notified by providing a way for publishers to "register". When an event occurs. The publisher "triggers the event" and then executes all the events submitted by the Subscriber.
2.2 Events
Events are like simple delegates that are dedicated to a particular purpose. The event contains a private delegate.
The # event provides structured access to its private control delegate. In other words, you cannot access the delegate directly.
# The actions available in the event are less than delegates, and for events we can only add, delete, or invoke event handlers. Only use "+ =,-="
# When the event is triggered, it invokes the delegate to invoke the method in the invocation list in turn.
Delegate voidHandler ();//declaring a delegate//published by classIncrementer {pulbicEventHandler Countedadozen;//Create events and publish Public voidDocount () { for(inti =1; I < -; i++) ifI A==0&& Countedadozen! =NULL) Countedadozen (); } } //subscribed by classDozens { Public intDozenscount {Get;Private Set; } Publicdozens (Incrementer incrementer) {Dozenscount=0; Incrementer. Countedadozen+ = Incrementdozenscount;//Subscribe to Events } voidIncrementdozenscount ()//declaring event handlers{Dozenscount++; } }
class program { staticvoid Main () { new incrementer (); New dozens (incrementer); Incrementer. Docount (); Console.WriteLine ("{0}", Dozenscounter.dozenscount); } }
2.3 Standard Events
Delegate types for System namespace declarations
public delegate void EventHandler (object sender, EventArgs e);
The EventArgs class is declared in the System namespace and is designed to pass any data without parameters and is usually ignored. If you want to pass data, you must declare a class derived from EventArgs and use the appropriate fields to hold the data.
classIncrementer { Public EventEventHandler Countedadozen;//using system-defined EventHandler Public voidDocount () { for(inti =1; I < -; i++) ifI A==0&& Countedadozen! =NULL) Countedadozen ( This,NULL);//use parameters when triggering events } } classDozens { Public intdozenscount{Get;Private Set;} Publicdozens (Incrementer incrementer) {Dozenscount=0; Incrementer. Countedadozen+=Incrementdozenscount; } voidIncrementdozenscount (Objectsource, EventArgs E) {Dozenscount++; } }
C # notes-Delegates & Events