Why is Action/Func better than a regular Method in .Net?

前端 未结 5 450
滥情空心
滥情空心 2020-12-28 19:28

I much prefer using an Action or a Func if I need a quick reusable piece of code, however others on my team don\'t like them or understand them.
At the moment my only re

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-28 19:34

    Pros

    • Closures
      • This does not help your case though.
    • Super-private methods -- only the defining method can call it.
      • This is easily countered with the Single Responsibility Principle though. You're likely combining what should be a different class in an entire method.
    • Callbacks
      • These can also easily be methods.

    Cons

    • Readability is lessened, in my opinion, because of the excess indentation (I'm not a fan).
    • Debugging becomes a little harder because your call stack is less intuitive especially when you have multiple Func/Action's defined.

    You'll have to over come the cons in your environment.

    I'm curious about your "more up-to-date coding practice" argument. Just because you can do this as much as possible doesn't mean you should.

提交回复
热议问题