私はC#を学んでいます。 a construct that can adopt the changes in the environment in which it is defined.
例 :
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; });
}
変数id
は FindPersonByID() のスコープで宣言されていますid
が、無名関数内のローカル変数にアクセスできます (つまり)delegate(Person x) { return x.id == id; }
(1) 閉鎖についての私の理解は正しいですか?
(2) 閉鎖によって得られる利点は何ですか?