C # commissioned study

------------ ------------ restore content begins

A system built (predefined) delegate type

  1. Action delegate
    1. With no return value
    2. You can point to a no return value, a method without parameters
    3.  1 class Program
       2     {
       3         static void PrintString()
       4         {
       5             Console.WriteLine("hello world.");
       6         }
       7         static void PrintInt(int i)
       8         {
       9             Console.WriteLine(i);
      10         }
      11         static void PrintString(string str)
      12         {
      13             Console.WriteLine(str);
      14         }
      15 
      16         static void PrintDoubleInt(int x, int y)
      17         {
      18             Console.WriteLine(x + " " + y);
      19         }
      20         /// <summary>
      21         /// Action委托不带返回值
      22         /// </summary>
      23         /// <param name="args"></param>
      24         static void Main(string[] args)
      25         {
      26             //Action a = PrintString; //a delegate type system action is built (predefined), he can not return the value of a point, a method without parameters
       27  
      28              // the Action <int> A = PrintInt; // defines a delegate type, which can point to a no return value, a method parameter int
       29  
      30              // the Action <string> = PrintString at a; // defined in oh a delegate type, which can not point to a return value, the method has the string parameter, where the system It will automatically find matching method 
      31 is  
      32              the Action < int , int > a = PrintDoubleInt;
       33 is              a ( 24 , 45 );
       34 is  
      35              the Console.ReadKey ();
       36              // action can be directed back to the action specified by the method of the generic a plurality of types of parameters, with the type of the delegate type of parameter is specified later corresponding to 
      37          }
      38     }

       

    4. Can point to a value not return, there is a method of one or more parameters (up to 16 parameters)
  2. Func delegate
    1. It must contain a return value
    2. The last type indicates the type of the return value, a parameter indicating the type of front
      • . 1  the using the System;
         2  the using the System.Collections.Generic;
         . 3  the using the System.Linq;
         . 4  the using the System.Text;
         . 5  
        . 6  namespace _00_Func commissioned
         . 7  {
         . 8      ///  <Summary> 
        . 9      /// Func return value will contain a
         10      / // the last type indicates the type of the return value, a parameter indicating the foregoing type
         . 11      ///  </ Summary> 
        12 is      class Program
         13 is      {
         14          static  int Test1 ()
         15          {
         16              return  . 1 ;
        17         }
        18         static int Test2(string str)
        19         {
        20             Console.WriteLine(str);
        21             return 100;
        22         }
        23         static int Test3(int i, int j)
        24         {
        25             return i + j;
        26         }
        27         static void Main(string[] args)
        28         {
        29             //Func<int> a = Test1;//func generic type specified in the method return type hair
         30              // Console.WriteLine (A ());
         31 is  
        32              // Func <String, int> A = Test2; // later with many types Func can last type is the type of a return value foregoing type parameter type, parameter types must correspond with the order parameter type pointing method 
        33 is  
        34 is              func < int , int , int > a = the Test3; // back a return value must be specified func type, parameters can have 0-16 parameter first type, the last one is the return value of type 
        35              int AA a = ( . 1 , . 5 );
         36  
        37 [              // Console.WriteLine (a ( "big read")); 
        38              the Console.ReadKey ();
         39          }
         40      }
        41  
         

         

Guess you like

Origin www.cnblogs.com/colorzore/p/12081886.html