<分区> 分区>
代码:
using System;
using System.Collections.Generic;
namespace so {
public abstract class Feature {
public void doIt() {
Console.WriteLine( GetType().FullName );
}
}
class A : Feature { }
class B : Feature { }
class C : Feature { }
public class SSCCE {
event EventHandler Click;
static void Main( string[] args ) {
SSCCE sscce = new SSCCE();
List<Feature> features = new List<Feature>();
features.Add( new A());
features.Add( new B() );
features.Add( new C() );
foreach ( Feature feature in features ) {
sscce.Click += ( object sender, EventArgs e ) => { feature.doIt(); };
}
sscce.Click.Invoke( null, null );
}
}
}
预期结果:
so.A
so.B
so.C
观察结果:
so.C
so.C
so.C
在java中,foreach循环中Feature前面的final关键字允许feature值在 .doIt() 之前,在 lambda 的 Action 主体中使用。
C# 中好的语法是什么?
