《C#高级编程》中有个例子添加事件时用匿名方法:
btnOne.Click+=newEventHandler(lblInfo.Text="button1waspressed.";);
调试时提示错误:
错误1应输入)
错误2无效的表达式项“)”
请问这是为什么啊
【amandag】:
btnOne.Click+=newEventHandler(delegate(){lblInfo.Text="button1waspressed.";});
【zhoufoxcn】:
btnOne.Click+=newEventHandler(SetText());
privatevoidSetText()
{
lblInfo.Text="button1waspressed.";
}
【zhoufoxcn】:
btnOne.Click+=newEventHandler(delegate(){lblInfo.Text="button1waspressed.";});
这种办法也行。
【sparksharp】:
这才是正确的匿名方法方式:
btnOne.Click+=newEventHandler(delegate(objectsender,EventArgse){lblInfo.Text="button1waspressed.";});
//EventHandler委托原型的参数列表必须为:(objectsender,EventArgse)
时间: 2024-12-29 13:13:27