C#Lambda表达式:我为什么要使用它们?我很快就读了一遍MicrosoftLambda表达式文件。然而,这种例子帮助我更好地理解了:delegate int del(int i);del myDelegate = x => x * x;int j = myDelegate(5); //j = 25尽管如此,我还是不明白为什么这是一种创新。它只是一种在“方法变量”结束时死亡的方法,对吗?为什么我要用这个而不是真正的方法呢?
3 回答
qq_花开花谢_0
TA贡献1835条经验 获得超7个赞
// anonymous delegatevar evens = Enumerable .Range(1, 100) .Where(delegate(int x) { return (x % 2) == 0; }) .ToList();// lambda expressionvar evens = Enumerable .Range(1, 100) .Where(x => (x % 2) == 0) .ToList();
Expression<T>
void Example(Predicate<int> aDelegate);
Example(x => x > 5);
void Example(Expression<Predicate<int>> expressionTree);
x > 5
HUWWW
TA贡献1874条经验 获得超12个赞
private ComboBox combo;private Label label;public CreateControls(){ combo = new ComboBox(); label = new Label(); //some initializing code combo.SelectedIndexChanged += new EventHandler(combo_SelectedIndexChanged);}void combo_SelectedIndexChanged(object sender, EventArgs e){ label.Text = combo.SelectedValue;}
public CreateControls(){ ComboBox combo = new ComboBox(); Label label = new Label(); //some initializing code combo.SelectedIndexChanged += (s, e) => {label.Text = combo.SelectedValue;};}
- 3 回答
- 0 关注
- 1458 浏览
添加回答
举报
0/150
提交
取消