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

前端 未结 7 1518
别跟我提以往
别跟我提以往 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:52

    Lambda expression is not (and was not meant to be) a silver bullet that would replace (hide) delegates. It is great with small local things like:

    List names = GetNames();
    names.ForEach(Console.WriteLine);
    
    1. it makes code more readable thus simple to understand.
    2. It makes code shorter thus less work for us ;)

    On the other hand it is very simple to misuse them. Long or/and complex lambda expressions are tending to be:

    1. Hard to understand for new developers
    2. Less object oriented
    3. Much harder to read

    So “does it mean we don’t have to use delegates or anonymous methods anymore?” No – use Lambda expression where you win time/readability otherwise consider using delegates.

提交回复
热议问题