Advantages of using delegates?

前端 未结 14 1357
渐次进展
渐次进展 2020-12-13 11:39

I\'m looking to implement the Observer pattern in VB.NET or C# or some other first-class .NET language. I\'ve heard that delegates can be used for this, but can\'t figure ou

14条回答
  •  误落风尘
    2020-12-13 12:13

    You're not thinking like a programmer.

    The question is, Why would you call a function directly when you could call a delegate?

    A famous aphorism of David Wheeler goes: All problems in computer science can be solved by another level of indirection.

    I'm being a bit tongue-in-cheek. Obviously, you will call functions directly most of the time, especially within a module. But delegates are useful when a function needs to be invoked in a context where the containing object is not available (or relevant), such as event callbacks.

提交回复
热议问题