C # The thought of closure caused by variable capture

Source: Internet
Author: User

Objective

Occasionally flipping through the books to see the original rational things are really a bit dull, before you see a park in the garden friends said 3-6 years of work experience should be understood. NET knowledge, one point is about the closure in C #, in fact, early in the book (before I did not know that there is a closure in C #) to see the content of the closure is very small and the introduction of the example of a look to understand (finally, there is an impression), anyway, work and do not come to let you to achieve closure, So oneself have the luck psychology, this two days whim again turned over the book want to study carefully (perhaps out of the fear of it, work a few years unexpectedly do not know the closure, even if know and only to understand, you are self-deceiving), immediately after the study of the information to research this thing, if there are errors, please point out.

Topic

First of all, let's look at the evolutionary evolution of the Commission, and it is asked that the topic in this section is not "C # The thought of closures caused by variable capture"? Oh, look also carefully, yes, we can not worry about it, and someone asked, you did not write about the delegation of the detailed introduction, oh, it seems that my fans know a lot, but this introduction focus on different, nonsense less, into the theme is the truth.

The delegate of c#1.0

We know it today. A list can be implemented by lamda such as where or predicate, whereas in c#1.0 we have to write a method to implement the predicate logic, and the creation of the delegate instance is created by the specified method name. Let's create a helper Class (Listutil), as follows:

/// <summary>    ///Manipulating the list helper class/// </summary>    Static classListutil {/// <summary>        ///Create a predicate/// </summary>         Public StaticIlist<t> filter<t> (ilist<t> source, predicate<t>predicate) {List<T> ret =NewList<t>(); foreach(T Iteminchsource) {                if(predicate (item)) {Ret.                ADD (item); }            }            returnret; }        /// <summary>        ///traverse the list and print on the console/// </summary>         Public Static voidDump<t> (ilist<t>list) {            foreach(T Iteminchlist)            {Console.WriteLine (item);        } console.readkey (); }    }

At the same time, a test data is given:

    Static classSampleData { Public Static ReadOnlyilist<string> Words =Newlist<string> {" the","Quick","Brown","Fox","jumped",             " Over"," the","Lazy","Dog" }.        AsReadOnly (); }

Now all we have to do is return a string with a small length equal to 4 and print it, giving a method with a length less than 4:

        Static BOOL Matchfourlettersorfewer (string  Item)        {            return4;        }

Here we call the above method in the console to filter:

  predicate<stringnew predicate<string>(matchfourlettersorfewer);            IList<string> shortwords = listutil.filter (sampledata.words, predicate);            Listutil.dump (shortwords);

The results are printed as follows:

All of the above are so easy! when we use delegates to implement a simple one-time call is not used many times, in order to streamline the code, the anonymous method appears in C # 2.0.

The delegate of c#2.0

The above method of calling in the console can be modified to achieve the same effect as follows:

            predicate<string> predicate =                 delegate(string  Item)                 {                      Return4;                };            IList<string> shortwords = listutil.filter (sampledata.words, predicate);            Listutil.dump (shortwords);

Well, here seems a bit of a waste of space, to here we look at the above code, for predicate the length of the filter data is hard-coded, missing something, we first want to talk about the closure, What is needed for closures can be summarized as: Closures are entities that combine functions with their referencing environment (from: What you have to know.) NET). We can interpret it as a synthesis of functions and contexts. We need to give a context by manually entering the length of the filtered data. We give a filter-length class (Variablelengthmather):

     Public classVariablelengthmatcher {intmaxLength; /// <summary>        ///Pass the data that is entered manually/// </summary>        /// <param name= "MaxLength" ></param>         PublicVariablelengthmatcher (intmaxLength) {             This. MaxLength =maxLength; }        /// <summary>        ///similar to anonymous methods/// </summary>         Public BOOLMatch (stringItem) {            returnItem. Length <=maxLength; }    }

Let's do a manual input call to filter the data:

            Console.Write ("");             int int . Parse (Console.ReadLine ());             New Variablelengthmatcher (maxLength);            predicate<string> predicate = matcher. Match;            IList<string> shortwords = listutil.filter (sampledata.words, predicate);            Listutil.dump (shortwords);

The demo is as follows:

We then make the following modifications to the console code:

Console.Write ("Maximum length of string to include?"); intMaxLength =int.            Parse (Console.ReadLine ()); Variablelengthmatcher Matcher=NewVariablelengthmatcher (maxLength); predicate<string> predicate =Matcher.            Match; IList<string> shortwords =Listutil.filter (sampledata.words, predicate);            Listutil.dump (shortwords); Console.WriteLine ("Now for words with <= 5 letters:"); maxLength = 5;            Shortwords = Listutil.filter (sampledata.words, predicate); Listutil.dump (shortwords);

We just changed the MaxLength value and printed it again, and the results were as follows:

The delegate of c#3.0

For a better demonstration of the code, we use the LAMDA expression in c#3.0 to demonstrate, we continue to the above, when we change maxlength to 5 o'clock, when we filter the data and 4, at this time we use the anonymous method or the LAMDA expression as shown above, as shown below.

Console.Write ("Maximum length of string to include?"); intMaxLength =int.            Parse (Console.ReadLine ()); predicate<string> predicate = Item and item. Length <=maxLength; IList<string> shortwords =Listutil.filter (sampledata.words, predicate);            Listutil.dump (shortwords); Console.WriteLine ("Now for words with <= 5 letters:"); MaxLength=5; Shortwords=Listutil.filter (sampledata.words, predicate); Listutil.dump (shortwords);

Look at the demo results:

From the above demonstration results can be seen at this time the MaxLength is 5, of course, the results of print filtering is not the same, this time to talk about the first topic "Variable Capture." Both anonymous methods and lambda expressions in C # 2.0 and 3.0 can capture local variables.

So the question is, what is variable capture? How do we understand it?

Let's look at another example using lambda expressions:

            var " Cnblogs " ;            Func<String> capture = () = () = name;             " xpy0928 " ;            Print (Capture);
        Static void Print (func<string> capture)        {            Console.WriteLine (Capture ());            Console.readkey ();        }

So what will the printing result be? Cnblogs? xpy0928?

Name is captured, and the lambda also makes a corresponding change when the local variable changes (because lambda delays execution), so the xpy0928 is output. So what exactly does the compiler do to make the output xpy0928? Inside the compiler, the above code is converted roughly as follows.

     Public class Capture    {        publicstring  name;          Public string Printname ()        {            returnthis. Name;        }    }
    var New Capture ();     " Cnblogs " ;     " xpy0928 " ;    Print (capture.printname);

When we get here, we can understand the meaning of capturing variables. The lambda always points to the name value in the current object, that is, the reference in the object always exists in Lamda.

The next thing to say about closures is that we've been talking about variable capture before, because the source of the closure is the variable capture . (Personal understanding, if there are errors please correct).

The result of a variable capture is that the compiler produces an object and promotes the local variable to an instance variable to extend the life cycle of the local variable, which is called a closure.

What is the meaning of the above remark? Let's look at one more example:

            list<func<intnew list<func<int>>();              for (int0; j ++                ) (= j)            ; foreach (func<int in funcs)                Console.WriteLine (func ());            Console.readkey ();

Some people say the above example is a closure, yes, a closure and the result returns 10 10, well done! That's not it, we have to explain it. Let's look at it in a sentence.

  Funcs. ADD (() = j);

() What does =>j mean, let's take a look at the six evolution of the lambda expression before:

Instantiate an anonymous delegate and return the J value, note that () =>j is the current value of the return variable J rather than the return value J. The returned anonymous delegate is an anonymous class and accesses the attribute in this class J (why is the returned anonymous delegate an anonymous class, see this link: http://www.cnblogs.com/jujusharp/archive/2011/08/04/ c-sharp-and-closure.html). After that, let's explain why we're printing 10 of 10?

Each anonymous delegate created now captures this variable J, so each anonymous delegate, the anonymous class, keeps a reference to the field J, and when the For loop finishes 10 o'clock the field value becomes 10 until J is not referenced by the anonymous delegate, and J is recycled by the garbage collector.

Let's take a look at the changes above:

 list<func<int  >> funcs = ne            W  list<func<int  >> ();  for  (int  j = 0 ; J < 10 ; J++  J; Funcs.            Add (()  => TEMPJ);  foreach  (Func<int  > Fun            C in   Funcs) Console.WriteLine (func ()); Console.readkey ();  

It is obvious that the output is 0-9 because a temporary variable, TEMPJ, is created at this point, and the anonymous delegate, which is the lambda captures a different tempj each iteration, can then be output as we expect.

Let's look at one more situation:

              for (int0; j + +)            {                Func<int> Fun = () + J;                Console.WriteLine (Fun ());            }

This is still normal output 0-9, because at this point the lambda expression executes immediately after each iteration, instead of all lambda executions, as in the case of the first example until the loop is deferred until 10.

Summarize

Closure concept: Closures allow you to encapsulate some behavior, pass it like an object, and still be able to access the context of the original declaration at the first time. This allows the control structure, logical operations, and so on to be separated from the invocation details.

Role: (1) facilitates code simplification. (2) facilitates function programming. (3) code security.

Resources:

C # in Depth:http://csharpindepth.com/articles/chapter5/closures.aspx

Variable Capture in C # with Anonymous delegates:http://www.digitallycreated.net/blog/34/variable-capture-in-c%23- With-anonymous-delegates

Understanding Variable Capturing in c#:https://blogs.msdn.microsoft.com/matt/2008/03/01/ understanding-variable-capturing-in-c/

C # and closures: http://www.cnblogs.com/jujusharp/archive/2011/08/04/C-Sharp-And-Closure.html

C # The thought of closure caused by variable capture

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.