C# -Closure -Clarification

Posted by nettguy on Stack Overflow See other posts from Stack Overflow or by nettguy
Published on 2010-03-27T08:09:15Z Indexed on 2010/03/27 8:13 UTC
Read the original article Hit count: 394

Filed under:
|

I am learning C#.Can I mean closure as a construct that can adopt the changes in the environment in which it is defined.

Example :

List<Person> gurus = 
new List<Person>()
                 {
                  new Person{id=1,Name="Jon Skeet"},
                  new Person{id=2,Name="Marc Gravell"},
                  new Person{id=3,Name="Lasse"}
                 };            


void FindPersonByID(int id)
{
  gurus.FindAll(delegate(Person x) { return x.id == id; }); 
}

The variable id is declared in the scope of FindPersonByID() but t we still can access the local variable id inside the anonymous function (i.e) delegate(Person x) { return x.id == id; }

(1) Is my understanding of closure is correct ?

(2) What are the aditional advantages can we get from closures?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about closures