One of the core foundations of C # 1--delegation
One of the core foundations of C # 1--delegation
The composition of a simple delegate
Merging and deleting delegates
Simple discussion of events
Commission Summary
The composition of a simple delegate
Declaring the delegate type ;
delegate void StringProcessor(string input);
Find an appropriate method for the operation of the delegate instance
void PrintString(string x)
Fully meet the requirements
void PrintInteger(int x)
Parameter type is not compatible
void PrintTwoStrings(string x, string y)
The number of arguments does not match
int GetStringLength(string x)
return type mismatch
void PrintObject(object x)
C # 1 requires exactly the same parameter type, so although string is derived from object, it still does not match. But C # 2 improved the situation.
As with parameter types, in, out, and ref prefixes must also match, although rarely used.
Create a delegate instance
The specific form of expression used to create a delegate instance depends on whether the operation uses an instance method or a static method.
StringProcessor proc1, proc2;
proc1 = new StringProcessor(StaticMethods.PrintString);// 静态方法
InstanceMethod instance = new InstanceMethod();
proc2 = new StringProcessor(instance.PrintString);//实例方法
Invoking a delegate instance
Handles calls to delegate instances that use the C # simplified syntax.
The essence of a delegate is to perform some kind of operation indirectly .
Merging and deleting delegates
The delegate instance actually has an action list called the invocation list of the delegate instance.
System.Delegate
A static method of type Combine
and Remove
is responsible for creating a new delegate instance. Rarely called directly, generally using +/+= and-/-= operators.
When a delegate instance is invoked, all its operations are executed sequentially, and the return value of Invoke is the return value of the last operation. If any action in the invocation list throws an exception, subsequent operations are prevented.
Simple discussion of events
The event is not a field of a delegate type. Events can be seen as similar to attributes (essentially, methods).
- Both are declared to have a specific type, and the event must be a delegate type.
- The essence of a property is the invocation of a value method and an assignment method. Similarly, when subscribing to or canceling an event, it looks like a field of the delegate type is used with the + = and-= operators, and the add and remove methods are actually called.
- The first reason for the existence of an "event" is similar to the "attribute"--adding a wrapper layer to implement the publish/subscribe pattern.
Commission Summary
- A delegate encapsulates a behavior that contains a special return type and a set of parameters, similar to an interface that contains a single method.
- The signature in the delegate type declaration determines which methods can be used to create the delegate instance, and also determines the signature of the call.
- Creating a delegate instance requires a method and the target of the calling method (for instance methods).
- The delegate instance is immutable (once created, it cannot be changed.) Don't worry about consistency and thread safety).
- Each delegate instance contains a list of calls.
- Delegate instances can be merged together, or they can be removed from one delegate instance China.
- The event is not a delegate instance-it's just a pair of add/remove (a method/assignment method for similar attributes).
From for notes (Wiz)
One of the core foundations of C # 1--delegation