How to Create C # Closure ?,

Source: Internet
Author: User

How to Create C # Closure ?,

An important concept in JavaScript is closure. Closure has a large number of applications in JavaScript, but do you know? C # You can also create Closure. The following describes how to create a magic closure in C.

Before that, we must first know how to define functions in C #.

1 // function definition. The parameter is string and the return value is string2 Func <string, string> myFunc = delegate (string msg) 3 {4 return "Msg:" + msg; 5 };

Lambda expressions can simplify the above Code, but the effect is the same:

1  //Lambda2  Func<string, string> myFuncSame = msg => "Msg:" + msg;

After defining a function, you can call it:

1 // function call 2 string message = myFuncSame ("Hello world ");

Defines a nested function with external variables (relative to embedded functions). External functions return nested functions:

1 public static Func <int, int> Func () 2 {3 var myVar = 1; 4 Func <int, int> inc = delegate (int var1) 5 {6 // myVar can record the status (value) after the last call. 7 myVar = myVar + 1; 8 return var1 + myVar; 9}; 10 return inc; 11}

C # Closure is called as follows:

1 static void CsharpClosures()2 {3     var inc = Func();4     Console.WriteLine(inc(5));//75     Console.WriteLine(inc(6));//96 }

When inc (6) is called for the second time, the variable myVar in the function does not reinitialize (var myVar = 1) as it does when the function is called for the first time, but retains the value of the first operation, 2. Therefore, the result returned by inc (6) is (2 + 1 + 6) = 9.

The level is limited. Hope you can kindly advise me! If you think it is good, please click recommendation and follow!
Source: http://www.cnblogs.com/isaboy/
Disclaimer: The copyright of this article is shared by the author and the blog Park. You are welcome to reprint it. However, you must keep this statement without the author's consent and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.

Related Article

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.