这在我的另一个问题中提到过,我认为将它添加到记录中可能会有用.在下面的程序中,本地定义的委托(如果有的话)在调用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 }); } } }