《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)