C # Closure

Source: Internet
Author: User

First look at the program:

Code

 Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace shortletets
{
Class Program
{
Static void Main (string [] args)
{
String srcStr = "Xinzhou district _ Wuhan, Huangpu District _ Shanghai, Hongshan district _ Wuhan, chong'an district _ Wuxi, Huangpi District _ Wuhan, binhu district _ Wuxi ";
String cityName = "Wuhan, Wuxi, Shanghai ";

String [] citysArr = cityName. Split (',');
IEnumerable <string> [] areaName = new IEnumerable <string> [citysArr. Length];
Int count = 0;
For (; count <citysArr. Length; count ++)
{
AreaName [count] = srcStr. Split (','). Where (a => a. Contains (citysArr [count]). Distinct ();
}
// Console. WriteLine (count );
Console. Read ();
}
}
}

At last, the value of areaname [0], areaname [1], and areaname [2] is "Huangpu District _ Shanghai ".

Code

 Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace shortletets
{
Class Program
{
Static void main (string [] ARGs)
{
String srcstr = "Xinzhou district _ Wuhan, Huangpu District _ Shanghai, Hongshan district _ Wuhan, chong'an district _ Wuxi, Huangpi District _ Wuhan, binhu district _ Wuxi ";
String cityname = "Wuhan, Wuxi, Shanghai ";

String [] citysarr = cityname. Split (',');
Ienumerable <string> [] areaname = new ienumerable <string> [citysarr. Length];
Int COUNT = 0;
For (; count <citysarr. length; count ++)
{
Int temp = count;
AreaName [count] = srcStr. Split (','). Where (a => a. stinins (citysArr [temp]). Distinct ();
}
// Console. WriteLine (count );
Console. Read ();

}
}
}

You can get the desired result.

My post:

Http://topic.csdn.net/u/20091214/23/dddde3da-a064-436a-bf47-830dc80b7690.html

I haven't fully figured it out yet. I dare not make a final conclusion. Leave a space first.

Articles and discussions on similar issues:

Http://topic.csdn.net/u/20090821/13/7c30e8cb-3d37-4d4f-9c11-0df1dd7be8f4.html

Http://www.cnblogs.com/foundation/archive/2008/12/11/1352797.html

Http://www.cnblogs.com/qishichang/archive/2009/06/20/1507540.html

Http://www.cnblogs.com/Klesh/archive/2008/05/15/The-Beauty-of-Closures.html

Http://www.kuqin.com/dotnet/20080421/7169.html

I agree with the opinion in the second link:

"If a variable is referenced in an anonymous method, the local variable is promoted to an instance variable and stored in a closure object. After the upgrade, the variable will not die even if the method for creating the variable is executed. When all references to the anonymous function disappear, the closure variable can be Normally recycled by the garbage collector. Parameters or internal variables in the closure are not placed in the stack, but in a global table during program execution.

Local variables extend the life cycle by changing to the instance field of the object. The compiler automatically generates Nested classes and gives the value of local variables to the objects of the nested classes. When the methods are called, the values are extracted from the objects ."

Supplement:

SrcStr. split (','). where (a =>. contains (citysArr [temp]). distinct (); is an anonymous method. The correct result can be obtained by using temp because temp reopens up space every time.

What is a closure?

To put it simply, the closure allows you to encapsulate some behaviors and deliver them like an object, and it can still access the context of the first declaration. In this way, the control structure and logical operations can be separated from the call details. The ability to access the original context is an important feature of the difference between closures and general objects, although there are only some compiler skills in implementation.

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.