Advantages of using delegates?

前端 未结 14 1360
渐次进展
渐次进展 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:02

    Delegates are strong typing for function/method interfaces.

    If your language takes the position that there should be strong typing, and that it has first-class functions (both of which C# does), then it would be inconsistent to not have delegates.

    Consider any method that takes a delegate. If you didn't have a delegate, how would you pass something to it? And how would the the callee have any guarantees about its type?

提交回复
热议问题