The easiest entry and understanding of C # delegate

 
  A delegate is a very important concept .net language, beginners do not really understand it does not matter, and after a time the research will be commissioned to understand the subtleties, .net can be said that there is no commission would be no more advanced behind the event, asynchronous forming properties may be multi-threaded, and so on. It must be taken seriously this important concept .net language.
  Delegate instance method is a pointer that points to one or a set of methods, call Invoke a delegate () method will bring it points to that method is executed again
  to pay attention to the understanding of the delegation and a delegate instance beginner, delegates are bound by it directed to a method returns the values and parameters, we declare variables corresponding to the manipulative variable; delegate instance variable is equivalent entrusted with specific constraints, it instantiates a delegate instantiation corresponding to such examples we when a class with a new class1 (); work in this way.
  Can put aside the concept of generics, or we'll be eating in the hills is even greater, and generics are only appeared late .net, commissioned by the concept already used in earlier will not have a generic very common. 
. 1      public  class calculator Class
 2      {
 3          // declare a particular delegate type mydelegate, it is bound to its predetermined variables of this type of the return value is a decimal type
 4          // It also bound to it a predetermined a method of this type of return value is a variable of type decimal two 
. 5          public  the delegate  decimal MyDelegate ( decimal m, decimal n-);
 . 6          // Next there are two methods meet the parameters and return values of the above specification: 
. 7          static  decimal Operation1 ( decimal X, decimal Y)
 . 8          { return X + Y;}
 . 9          static  decimal operation2 (decimal X, decimal Y)
 10          { return X * Y;}
 . 11          public MyDelegate algorithm;
 12 is          void XX ()
 13 is          {
 14              // Next is an example of a delegate, the method is also bound to a specific delegate type (MyDelegate) variable (algorithm) 
15              algorithm = new new MyDelegate (Operation1);
 16              // use + = operator to bind a plurality of methods to delegate type (MyDelegate) variable 
17              algorithm = + new new MyDelegate (operation2);
 18 is          }
 . 19          public  decimal ExecuteOperation ( decimal A,decimal B)
 20 is          {
 21 is              return algorithm .Invoke (A, B);
 22 is          }
 23 is      }
 24      // 2. embodied delegate object-oriented polymorphism: In the following calculation the client class we only need to pass the class calculator [algorithm] delegate instance a specific algorithm (subtraction),
 25      // the calculator can have classes without modification new algorithm capability 
26 is      public  class client computing
 27      {
 28          calculator class = jsq1 new new calculation class ();
 29          // calculator and no new class of algorithms: a subtraction 
30          public  decimal subtraction ( decimal X, decimal Y)
 31 is          { return X -Y;}
 32          void indication calculator subtraction ()
 33          {
 34              // binds to a delegate instance of method 
35              jsq1 + = Algorithm. subtraction;
 36              Console.WriteLine (. jsq1 algorithm .Invoke ( . 5 , . 6 ));
 37          }
 38 is  
39  
40          // we can also do this, the algorithm calls an external method packaged, then bind to members of a delegate instance of this method, such as 
41 is          void indication calculator to do the divisions ()
 42          {
 43              // the individual class the method of binding members to delegate instance 
44 is              jsq1 algorithm = +. new new division algorithm based () divided;.
 45              . Console.WriteLine (jsq1 algorithm .Invoke ( 20 is, 5 ));
 46          }
 47      }
 48      // the method of division members packaged in a separate class: division 
49      public  class division algorithm based
 50      {
 51 is          public  decimal division ( decimal X, decimal Y)
 52 is          { return X / Y;}
 53 is      }

 If understanding the wrong place, welcome to correct me Paizhuan.

Guess you like

Origin www.cnblogs.com/hrx521/p/12128719.html