简体   繁体   中英

c# KeyEventArgs with lambda expression

Is this possible to write with lambda expression?

I tried private void CancelBtn_Click(object sender, EventArgs e) => Close(); and it works.

private void UpdateEmployees_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Escape)
        {
            Environment.Exit(0);
        }
    }

You can write a lambda expression by passing an input parameters to handle OnKeyDown event, without declaring UpdateEmployees_KeyDown method

UpdateEmployees.OnKeyDown += (sender, e) =>
{
    if (e.KeyCode == Keys.Escape)
    {
        Environment.Exit(0);
    }
}

Your first expression private void CancelBtn_Click(object sender, EventArgs e) => Close(); isn't a lambda expression, it's an expression-bodied method , which is available starting from C# 6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM