10.c# variable capture of anonymous functions (five chapter 5.5)

Source: Internet
Author: User

First of all, thank the Park Friends of the designation, follow-up articles will be more than a yard to think, out of the article only convincing. Well, let's talk about it today. Anonymous functions, for anonymous functions, we know that using the delegate keyword, we need to know that the anonymous function in the variable is the way to handle, first of all two terms, external variables and captured external variables, you can see "external variables captured = External variables + captured", This capture, as the name implies, uses this variable in an anonymous function.

  An external variable: An anonymous method inside an instance member of a class that refers to a variable or parameter within a scope that contains an anonymous method, which is also considered an external variable.

  captured external variable : it refers to an external variable that is used in an anonymous method.

The code is as follows

1 Static voidMain (string[] args)2 {3     //x and y are called external variables4     intx =0, y =1;5     //when x is used in an anonymous method, X is called the captured external variable6action<int> AC =Delegate(intN) {Console.WriteLine (x);};7 8     //Summary: x, Y, and anonymous methods are all within the scope of the main function and can also be extended to the scope of the class and the scope of the namespace9 Ten Console.readkey (); One}

Again, the anonymous method captures the behavior of the variable, you can see in the anonymous method we access to the local variable x, note, not only access to the value of X, but in the anonymous type using a type instance reference to the variable x, for the x change, because it is a reference, so always use this type instance access to, such as

1 Long  One  A ; 2 action<longdelegate (long  l) {Console.WriteLine (x1);}; 3 ac1 (1L// print4 x1 = y1; 5 ac1 (1L);  // Print

Parameter long L is not used here, but the parameter here is not the external variable mentioned above, because it is really the parameter of the anonymous method

1 Static void Debug (int  x) {2     action<intdelegate (int  y) { Console.WriteLine (x); }; 3 }

The above x is the external variable in the term, so it should be OK to distinguish the definition.

As for the lifetime of a variable, it can be in only one scope, and when the code executes the scope, the variables within that scope will be destroyed, but using an anonymous method can prolong the lifetime of the variable.

1 Static voidMain (string[] args)2 {3Getlen gl =GetMethod ();4gl"s");//Print 00s5gl"s");//Print 0000s6 7 Console.readkey ();8 }9 Ten  Public Delegate intGetlen (strings); One StaticGetlen GetMethod () A { -     stringtemp ="0"; -     return Delegate(strings) { thetemp =String.Concat (temp, temp); -s =String.Concat (temp, s); - Console.WriteLine (s); -         returns.length; +     }; -}

See Using GetMethod to return a delegate, where an anonymous function is used (because an anonymous function is the delegate of the corresponding signature), under normal comprehension, temp is in the GetMethod scope, and when it is outside the domain, the variable is destroyed, but the anonymous function uses a type instance to refer to the variable. This variable will not be destroyed, only if the anonymous function is destroyed (that is, the delegate) will be destroyed, thereby extending the scope of the variable, and the operation of the TEMP variable will be directly reflected on the variable referenced by the instance, such as the first call to GL ("s"), temp= "00", the second call, temp= "0000".

The last thing that is a bit around, is the instantiation of variables in the anonymous function of the access rules, but the personal feeling this is not very around, it is quite good understanding. Look at the code.

1Getlen[] A = { };2 intxx =0;3  for(inti =0; I <3; i++)4 {5     intxxx =i;6A[i] =Delegate(strings) {7xx++;8xxx++;9         returnXX +xxx;Ten     }; One}

A is a delegate array, for each delegate in the array share an XX instance reference, and each delegate each has a XXX instance reference (XXX corresponds to different delegates is different), because in the loop, each time the loop is instantiated xxx, For each delegate there is a new XXX instance reference, and xx is instantiated outside the loop, and each delegate shares an instance reference. Of course, in the actual use of the process, it can not be so simple, so we have to start the brain, a good distinction between which shared, which is quoted alone.

Please treatise.

10.c# variable capture for anonymous functions (Chapter five 5.5)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.