Is there a case where delegate syntax is preferred

2019-03-18 09:24发布

With the advent of new features like lambda expressions (inline code), does it mean we dont have to use delegates or anonymous methods anymore? In almost all the samples I have seen, it is for rewriting using the new syntax.

Any place where we still have to use delegates and lambda expressions won't work?

7条回答
老娘就宠你
2楼-- · 2019-03-18 09:39

Lamda's are just syntactic sugar for delegates, they are not just inline, you can do the following:

s.Find(a =>
{
    if (a.StartsWith("H"))
        return a.Equals("HI");
    else
        return !a.Equals("FOO");
});

And delegates are still used when defining events, or when you have lots of arguments and want to actually strongly type the method being called.

查看更多
登录 后发表回答