Is there a case where delegate syntax is preferred over lambda expression for anonymous methods?

前端 未结 7 1538
别跟我提以往
别跟我提以往 2021-02-07 21:13

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

7条回答
  •  时光取名叫无心
    2021-02-07 21:50

    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.

提交回复
热议问题