Why do we need C# delegates

前端 未结 8 610
遥遥无期
遥遥无期 2020-12-22 19:47

I never seem to understand why we need delegates? I know they are immutable reference types that hold reference of a method but why can\'t we just call the method directly,

8条回答
  •  隐瞒了意图╮
    2020-12-22 20:31

    Simple answer: the code needing to perform the action doesn't know the method to call when it's written. You can only call the method directly if you know at compile-time which method to call, right? So if you want to abstract out the idea of "perform action X at the appropriate time" you need some representation of the action, so that the method calling the action doesn't need to know the exact implementation ahead of time.

    For example:

    • Enumerable.Select in LINQ can't know the projection you want to use unless you tell it
    • The author of Button didn't know what you want the action to be when the user clicks on it
    • If a new Thread only ever did one thing, it would be pretty boring...

    It may help you to think of delegates as being like single-method interfaces, but with a lot of language syntax to make them easy to use, and funky support for asynchronous execution and multicasting.

提交回复
热议问题