The origin of the

closure to form a closure of

has some unnecessary conditions to summarize: the

1, the nested function.

2, anonymous function.

3, function as a parameter or return value.

4, in.NET, can form a closure by anonymously delegate: a function can be passed as a parameter or returned as a return value, or as a function variable. In.NET, this can be achieved by delegation. These are the prerequisites for the implementation of the closure.

wants to say that the origin of closure will have to say functional programming first. In recent years, functional programming is relatively hot. Let's take a look at some basic characteristics of functional programming, which helps us understand the origin of closure.

functional programming

functional programming is a programming model, which sees computer operations as a calculation of functions in mathematics and avoids the concept of state and variables. It is obvious here that the most important thing in functional programming is the function and a function in mathematics, such as f (x). The biggest feature of the function in mathematics is that as long as the same parameter x, then my result must be equal, that is to say, the return value of our function is only dependent on the parameters and not on other states. (for example, the global variable in JS is a disturbing factor); in the latter sentence, the concept of avoiding variables, which is not appropriate in functional programming, because the function in this sentence or the variable we use in the programming language is a storage unit, and the variable in the functional programming is a number. The definition of a variable in learning is a name worthy of a name. For example, our most basic assignment equation x = x+1 allows the programmer to see that this is a simple assignment code, and that the equation is not true for those who learn mathematics. So we do not allow multiple assignments in functional programming. This is also the most important reason for the benefits of functional programming:

first, the result of the function only depends on the parameters and does not rely on other states, so that the code is easy to make a mistake, a very convenient unit test and test.

second points, because of the immutability and stateless, then we do not have to worry about resource contention between multiple threads, and do not need to save the state with a lock.

high order function

functional programming function is a first-class citizen, and our slogan "everything is object" a bit similar. In functional programming, we try to use function to express everything, of course, we also need functions to pass through the past. This is the higher order function, that is, the function is used as a parameter or a return value, and then the multiplexing is realized, so that the size of the reuse can be reduced to function. There are similar things in the C# language - delegate, of course, the functions in C# are not the same as in functional programming, but there are features in some functional programming languages, such as lamda, Linq in C#.

about functional programming blog Garden, there are many good articles to introduce, I did not specify, next is to draw our theme today - closure.

because the basis of functional programming is the Lambda calculus, so this section shows us using the Lambda calculus to bring out our subject, and I know not too much about this calculus. Students who want to get started can see this point here

first define a simple calculus

X X. [lambda] y.x+y

if x is 1 y is a

X. ([lambda X. lambda y.x+y) 1) 2= ([lambda y.1+y) 2=. The outer function uses the inner layer function, that is, using a function as the result of calculation. OK, let's take the inner function alone, (lambda x. X + y), and you can see clearly that if the context off, our y is no value, that is, y is not bound, and that y is free for our function! If in functional programming, our y variable should be destroyed after the completion of the outer function, so what if we need to use y in the inner function if we need to use y? For this problem, the designer does other processes: if one function returns to another function, and the return function needs the variable of the outer function, it does not immediately release the variable, but allows the returned functions to refer to these variables. The language that supports this mechanism is called the support of the closure, and the internal function, together with its free variable, forms a closure (the sentence is taken from other blogs, and it is rare to sort out the text. " ). This is the origin of our closures, and the other languages, if they are useful to functional programming, and allow functions to be delivered will encounter similar problems, so each language needs to implement its own closures in its own way. The implementation of the closure of the packet in

C# from the previous section is that we can conclude that the closure is actually a code block that is to be executed and contains free variables (since the free variable is included in the code block, these free variables and the objects they reference are not released. " And a combination of computing environments that provide binding for free variables.

then enters our C# programming time, we use simple examples to implement, and look at the code generated by the compiler to see how the closures are implemented in the C#, after all, we also have delegated.

first writes a simple

 using System; namespace closure {static void} {static void} {{(1)); T; {/ / / / / / scope 2 return x + y;}}}}

can see that the scope of the incoming variable x in our test method is that when 1 executes the anonymous function, it should be released in the scope 2. Mg alt= "" src= " 2018419111526" >

shows that our variable X does exist in the scope 2. Let's look at what the compiler does for us.


can see that our test method is more than one object < > c__DisplayClass1_0 class_; and what is the specific definition of the ?

this is obvious, in fact, the closure is only the compiler to help us to encapsulate the free variable to an extraterritorial use for us in the image, if we remove it What about the use of the X variable in the scope 2?

compiler originally for free variable maintenance objects. The result is expected.

OK, this is the end of this article, about the closure is Python in the JS, or C# in C# I do not detail the


above is all the content of this article, I hope that the content of this article is for everyone's learning or work. A certain reference learning value, if there are doubts, you can leave a message, thank you for supporting the script home.

This paper fixed link: | Script Home | +Copy Link

Article reprint please specify:C# the closure of a new series of tutorials | Script Home

You may also be interested in these articles!