The World Cup round starts, connecting for three days, basically entering the world cup status. Watching the ball won't forget to play the technology. This time, I plan to summarize some of my experiences with c #, so that the vague concepts can be clarified. Let's join our friends!
[Closure] The first thought of getting into touch with this word is obscure. Let's take a look at it.
I. encounter [closure]
The first contact with closures is in js. First, let's look at the code segment [1]:
1 function a () {2 var I = 0; 3 function B () {4 alert (++ I); 5} 6 return B; 7} 8 var c = a (); 9 c ();Js Closure
The code is very simple. After careful observation, we will find that the scope of variable I is in method a. That is to say, variable I does not work after method a is generated, but the I in the code is still active in method c. Does it violate the basic rules of the program? Players playing the ball are still playing.
2. Face-to-face [closure]
The [Closure] is a regular expression that references free variables. It is usually a function (that is, function B in code snippet [1 ). It is characteristic that the referenced free variable will exist together with this function, even if it has left the environment where it was created, it is no exception, in general, closures extend the life cycle of variables. Is the code snippet [1] Clear?
The following code snippet of c # [2]:
1 public class Program 2 {3 public static Action funcB () 4 {5 Console. writeLine ("funcB Begin .. "); 6 int I = 0; 7 I ++; 8 Console. writeLine ("funcB:" + I); 9 Action action = () => 10 {11 Console. writeLine ("funcA:" + I); 12}; 13 I = 100; 14 Console. writeLine ("funcB:" + I); 15 Console. writeLine ("funcB End .. "); 16 return action; 17} 18 static void Main () 19 {20 var action = funcB (); 21 action (); 22 Console. readKey (); 23} 24}C # closure method 1
1 public class Program 2 {3 public static async void funcA (Action callback) 4 {5 // stay for 5 seconds, slow down the rhythm 6 await Task. delay (5000); 7 Console. writeLine ("funcA continue .. "); 8 callback (); 9} 10 public static void funcB () 11 {12 Console. writeLine ("funcB Begin .. "); 13 int I = 0; 14 I ++; 15 Console. writeLine ("funcB:" + I); 16 funcA () => 17 {18 Console. writeLine ("funcA:" + I); 19}); 20 I = 100; 21 Console. writeLine ("funcB:" + I); 22} 23 static void Main () 24 {25 funcB (); 26 Console. writeLine ("funcB End .. "); 27 Console. readKey (); 28} 29}C # closure 2
The two statements share the same purpose, that is, to bring up all your questions. What is the running result of the code segment [2? Why is this? [Closure] Does it really extend the life cycle of a variable? I believe that the people we are familiar with are clear. What we need to do is continue to dig deeper.
3. Dig deep into [closures]
We all know that this is just a syntactic sugar, and it does not violate the basic rules of the program. The following is a glimpse of the guy (. NET Reflector.
Figure [1] Figure [2]
Figure [3]
Figure [4]
Figure [5]
Let's take a look at five images.
Figure [1] shows a list of fields, methods, and classes of the Program class in Reflector. We noticed that, apart from the definition in our code, the compiler automatically generates a class: c _ DisplayClass1 !!
Figure [2] is a method <funcB> B _ 0 defined in Class c _ DisplayClass1 automatically generated by the compiler. It is actually the anonymous function () in the funcB method () ==>{ Console. implementation of WriteLine ("funcA:" + I;
Figure [3] is the IL code of the class c _ DisplayClass1 automatically generated by the compiler. Note the following methods: <funcB> B _ 0 and public variable I
Figure [4] and Figure [5] Are the IL code of the funB method. I have roughly labeled the meaning of each section. We can see that at the beginning of the method, the compiler initializes an instance of the c _ DisplayClass1 class and then operates on variable I, in IL, it is actually the operation of variable I in the global c _ DisplayClass1 class instance that was initially initialized. Therefore, [closure] prolongs the life cycle of variables, in fact, we have been operating on a global class instance variable.
4. Imitating [closures]
The principle is basically clear. Next we will simulate what the compiler is doing.
Code segment [3]:
1 public class Program 2 {3 // defines a global c _ DisplayClass1 type variable. 4 static c _ DisplayClass1 displayCls; 5 // <summary> 6 // This is the custom class similar to the compiler <> c _ DisplayClass1 7 // </summary> 8 sealed class c __ displayClass1 9 {10 public int I; 11 12 public void B _0 () 13 {14 Console. writeLine ("funcA:" + I); 15} 16} 17 public static Action funcB () 18 {19 displayCls = new c _ DisplayClass1 (); 20 Console. writeLine ("funcB Begin .. "); 21 displayCls. I = 0; 22 displayCls. I ++; 23 Console. writeLine ("funcB:" + displayCls. i); 24 Action action = displayCls. B _0; 25 displayCls. I = 100; 26 Console. writeLine ("funcB:" + displayCls. i); 27 Console. writeLine ("funcB End .. "); 28 return action; 29} 30 static void Main () 31 {32 var action = funcB (); 33 action (); 34 Console. readKey (); 35} 36}Similar to closures
The compiler has made us a syntactic sugar, making our programming easier and more elegant.
5. Ultimate imagination
Code only, code segment [4]:
1 public class Program 2 {3 public static List <Action> funcB () 4 {5 List <Action> list = new List <Action> (); 6 Console. writeLine ("funcB Begin .. "); 7 int I = 0; 8 I ++; 9 Console. writeLine ("funcB:" + I); 10 Action action1 = () => 11 {12 Console. writeLine ("funcA:" + I); 13 I = 200; 14}; 15 Action action2 = () => 16 {17 Console. writeLine ("funcA:" + I); 18}; 19 I = 100; 20 Console. writeLine ("funcB:" + I); 21 Console. writeLine ("funcB End .. "); 22 list. add (action1); 23 list. add (action2); 24 return list; 25} 26 static void Main () 27 {28 var action = funcB (); 29 action [0] (); 30 action [1] (); 31 Console. readKey (); 32} 33}Ultimate test
What is the running result? Do it yourself.
The title of the previous article is [from closure to delegate], because at this moment I naturally think of Delegate and want to play with it. For more information, see the next article!