C #6. Basic Knowledge,
C #6. Basic Knowledge
Prepared by Zhu, reproduced please indicate from Zhu jiayuan http://blog.csdn.net/zhgl7688
1. Anonymous method: it is an inline declaration method in the initialization delegate. It consists of delegate type keywords, parameter lists, and statement blocks ---- delegate (Parameters) {ImplementationCode }.
2. Anonymous method usage: it is used as the initialization expression when declaring the delegate variable, on the right side of the value assignment statement when combining the delegate, and on the right side of the assignment statement when adding events to the delegate.
3. The anonymous method parameter list must have three types of matching delegates: parameter quantity, parameter type, and modifier.
4. Anonymous Methods omit parameter list conditions: the list does not contain any out parameters and the method does not use any parameters.
5. The parameter list of the delegate Declaration contains the Params parameter. The params keyword is ignored in the anonymous method parameter list.
6. The scope of local variables in parameters and anonymous methods is limited to the subject of the implementation method.
7. The anonymous method can access the local variables and environment in the peripheral scope.
8. implementing external variables in anonymous methods is called method capture. Only the capture method is a part of the Delegate. Even if the variable has left the scope, the captured external variable is still valid.
9. Lambda expression: Replace the anonymous method, change delegate to operator =>, and simplify the operation. Example:
MyDel del = delegate (int x) {return x + 1 ;}; // anonymous method
MyDel del = (int x) =>{ return x + 1 ;}; // Lambda Method
MyDel del = (x) =>{ return x + 1 ;}; // Lambda Method
MyDel del = x =>{ return x + 1 ;}; // Lambda Method
MyDel del = x => x + 1; // Lambda Method
10. Key points of the Lambda expression list: parameters must match the delegate in quantity, type, and position. parameters do not have to contain types (except for out or ref, which are required) when a parameter is specified, parentheses can be omitted. When no parameter is specified, empty parentheses must be used.
11. trigger event: the term used to call or trigger an event. When an event is triggered, the methods registered to it are called in sequence. Equivalent to the observer mode: http://blog.csdn.net/zhgl7688/article/details/41969393.
12. Declare an event: only the delegate type and name are required. You can also change static to static.
For example, public event EventhandleElapsed // Eventhandle is the delegate type, and Elapsed is the event name.
13. An event is a member that is not a type and cannot use new to create its object. Its members are automatically initialized to NULL implicitly.
14. The event Delegate type uses the standard predefined EventHandler. Its declaration is as follows:
Public delegate void EventHandler (object sender, EventArgse );
15. subscribe to events: to add an event handler for an event, the handler must have the same response type and signature as the event Delegate, and use the + = Operator to add an event handler for the event. Use-= to remove an event handler.
16. Event methods include instance methods, static methods, anonymous methods, and lambda expressions.
Prepared by Zhu, reproduced please indicate from Zhu jiayuan http://blog.csdn.net/zhgl7688