// lamada expression is what? The left is the parameter list syntax goes to the right is the body of the method is essentially a method // lamada not trust, because trust is a type // nor commissioned example, is omitted here because new delegate () // His role: Yes a method // compiled: lamada actually generate a class in a class inside internal modification of the method, then the commission is bound to the static field of type
// declare a delegate: no parameters
Private delegate void DelegateMethod ();
// declare a delegate: with ginseng
Private delegate void DelegateMethod2 ( int the X-, int the y-);
public void the Test () { // 1.0 1.1 DelegateMethod Method = new new DelegateMethod ( a CallMethod ); // 2.0 callMethod method of copying blocks anonymous method, the private void callMethod into the delegate DelegateMethod method2 = new new DelegateMethod ( the delegate () { Console.Write ( " a CallMethod is performed 2 " ); }); // 3.0 delegate keyword removed and replaced in parentheses after the parameter write "=>" symbol DelegateMethod the method3 = new new DelegateMethod (() => { Console.Write ( " a CallMethod was performed 3 " ); }); // parameters form DelegateMethod2 method4 = new new DelegateMethod2 (( int X, int Y) => { Console.Write ($ " a CallMethod X is performed. 4: Y {X}: {Y} " ); }); // omit the parameter type automatic estimation of DelegateMethod2 method5 = new new DelegateMethod2 ((X, Y) => { Console.Write ($ " a CallMethod X is executed. 5: {Y} X: Y {} " ); }); // if only one line method thereof, and omit the braces semicolon DelegateMethod2 of method6 = new new DelegateMethod2 ((X, Y ) => Console.Write ($ " a CallMethod X is executed. 6: {Y} X: Y {} " )); // omitted new DelegateMethod2, compiler estimated DelegateMethod2 method7 = ((X, Y) => Console.Write ($ " a CallMethod X is executed. 6: {Y} X: Y {} " )); } // entrusted callback method Private void a CallMethod () { Console.Write ( " a CallMethod be performed "); } private void CallMethod2(int x, int y) { Console.Write("CallMethod2被执行 x:" + x.ToString() + " y:" + y.ToString()); }