[C #] Study Notes (1) Delegates, Events, Lambda Expressions

C # is followed by Yang tutorials go, here to thank the teacher's dedication, his cnblog address:> cgzl , his B station Address:> solenovex .

Into the title:

  Delegate represents delegate, delegate is a data structure that refers to a static method or reference examples and examples for class class. (English original words quoted an official document)

  Represents a delegate, which is a data structure that refers to a static method or to a class instance and an instance method of that class.

  I do not understand it does not matter, I can not read, ha ha ha.

  Take a look at two examples directly, the first Yang is provided.

  

. 1  the using the System;
 2  
. 3  namespace Demo
 . 4  {
 . 5      class Program
 . 6      {
 . 7          // define a class named delegate Transformer, the parameter type int, return type int. 
. 8          the delegate  int the Transformer ( int X);
 . 9          // define a static class Square, a Lambda expression used here, the same parameter is int, return type is int. 
10          static  int Square ( int X) => X * X; 
 . 11          static  void the Main ( String [] args)
 12 is          {
 13 is             T = Square the Transformer; // the Square method as a variable passed in delegate variable t, then create a delegate instance. 
14              int Result = T ( . 3 );   // call delegate method. 
15              Console.WriteLine (Result); // output: 9 
16          }
 17      }
 18 }

 Transformer where t = Square; a short, equivalent to the Transformer t = new Transformer (Square);

That is, when you call t (3) (a delegate instance), first call the delegate, delegate again invoke the target method. Draw a map should be well understood.

Advantages: decoupling.

Application: to write plug-in method

  · Methods is assigned to the delegate variable at run

  

. 1  the using the System;
 2  
. 3  namespace Demo
 . 4  {
 . 5      public  the delegate  int the Transformer ( int X); // define a delegate class to be noted that the type of incoming and return type. 
. 6  
. 7      class Util
 . 8      {
 . 9          public  static  void the Transform ( int [] values, the Transformer T) 
 10          {
 . 11              for ( int I = 0 ; I <values.Length; I ++ )
 12 is              {
 13 is                 values[i] = t(values[i]); // 委托实例t
14             }
15         }
16     }
17     class Program
18     {
19         static int Square(int x) => x * x;
20         public static void Main(string[] args)
21         {
22             int[] values = { 1, 2, 3 };
23             Util.Transform(values, Square); //Transform Util here call the static method and pass in parameters. The objective function Square 
24              the foreach ( int I in values)
 25              {
 26 is                  Console.WriteLine (I);
 27              }
 28          }
 29      }
 30 }

 

 Let's look at the example of official documentation to :> Delegate

 1 using System;
 2 
 3 namespace Demo
 4 {
 5     public delegate String myMethodDelegate(int myInt);
 6 
 7     public class mySampleClass
 8     {
 9         public String myStringMethod(int myInt)
10         {
11             if (myInt>0)
12             {
13                 return ("positive");
14             }
15             if (myInt<0)
16             {
17                 return ("negative");
18             }
19             return ("zero");
20         }
21         public static String mySignMethod(int myInt)
22         {
23             if (myInt > 0)
24             {
25                 return ("+");
26             }
27             if (myInt < 0)
28             {
29                 return ("-");
30             }
31             return ("");
32         }
33     }
34 
35     class Program
36     {
37         public static void Main(string[] args)
38         {
39             // Creates one delegate for each method. For the instance method, an
40             // instance (mySC) must be supplied. For the static method, use the
41             // class name.
42             mySampleClass mySC = new mySampleClass();
43             myMethodDelegate myD1 = new myMethodDelegate(mySC.myStringMethod);
44             myMethodDelegate myD2 = new myMethodDelegate(mySampleClass.mySignMethod);
45 
46             // Invokes the delegates.
47             Console.WriteLine("{0} is {1}; use the sign \"{2}\".", 5, myD1(5), myD2(5));
48             Console.WriteLine("{0} is {1}; use the sign \"{2}\".", -3, myD1(-3), myD2(-3));
49             Console.WriteLine("{0} is {1}; use the sign \"{2}\".", 0, myD1(0), myD2(0));
50         }
51     }
52 }

operation result:

Draw a map to analyze:

 

Multicast delegate

  Commissioned grabbed my own understanding is that the way people put me here, use the time to pick up, but I'm not here to take, but to others it go. em

   Multicast delegate is that you can use operator +, - to create a new delegate instance, and assigned to the current delegate variable.

   

using System;

namespace Demo
{
    public delegate int Transformer(int x);
    class Program
    {
        static int Square(int x)
        {
            var result = x * x;
            Console.WriteLine(result);
            return result;
        }
        static int Cube(int x)
        {
            var result = x * x * x;
            Console.WriteLine(result);
            return result;
        }
        public static void Main(string[] args)
        {
            Transformer t = null;
            t += Square;
            t += Cube;
            t(3);
        }
    }
}

Output:

Square earlier, it is first implemented.

If they have carried out - = removed, the final will be reported null pointer exception.

  At least one delegate instance in static object or an instance of an object, or throws a null pointer exception.

A combining operation returns null when the result of the operation is a delegate that does not reference at least one method.

"Delegates are immutable."

" As Combine  and Remove are actually creating a new delegate instance and assign it to the current delegate variable."

 

I feel that they have understood the wrong places, on the first so be it.

Guess you like

Origin www.cnblogs.com/braink-1400/p/11210935.html