我們應該檢查lambda表達式的傳入參數嗎? 換句話說,我們應該檢查參數o和s嗎?C# - 我們應該檢查lambda中的傳入參數嗎?
class MainWindow : Form /// implementation I
{
...
private ToolStripMenuItem mnuFileExit = new ToolStripMenuItem();
private void BuildMenus()
{
...
mnuFileExit.Click += (o, s) =>
{
MessageBox.Show(string.Format("{0} sent this event", o.ToString()));
Application.Exit();
};
...
}
...
}
class MainWindow : Form /// implementation II
{
...
private ToolStripMenuItem mnuFileExit = new ToolStripMenuItem();
private void BuildMenus()
{
...
mnuFileExit.Click += (o, s) =>
{
if (o != null)
{
MessageBox.Show(string.Format("{0} sent this event", o.ToString()));
Application.Exit();
}
};
...
}
...
}
只是一個簡短的問題,代表真的有必要嗎?從長遠來看,它可能會變成一種代碼味道。我的2美分。 – 2011-05-19 04:09:26
委託可以訪問您的BuildMenus方法中的本地變量。在某些情況下,這將非常方便。 – Allen 2011-05-19 04:22:22