これは私の他の質問で言及されており、記録に追加すると役立つかもしれないと思いました. 次のプログラムで、ローカルに定義されたデリゲートのうち、毎回最初から作成されるのではなく、Work メソッドの呼び出し間でキャッシュされるのはどれですか?
namespace Example
{
class Dummy
{
public int age;
}
class Program
{
private int field = 10;
static void Main(string[] args)
{
var p = new Program();
while (true)
{
p.Work();
}
}
void Work()
{
int local = 20;
Action a1 = () => Console.WriteLine(field);
Action a2 = () => Console.WriteLine(local);
Action a3 = () => Console.WriteLine(this.ToString());
Action a4 = () => Console.WriteLine(default(int));
Func<Dummy, Dummy, bool> dummyAgeMatch = (l, r) => l.age == r.age;
a1.Invoke();
a2.Invoke();
a3.Invoke();
a4.Invoke();
dummyAgeMatch.Invoke(new Dummy() { age = 1 }, new Dummy(){ age = 2 });
}
}
}