在C#中多路广播委托使用组播 [英] Use of multicast in C# multicast delegates
问题描述
当它是有用的具有多路广播委托过singlecast代表?
我用的代表很多,主要是加上C#lambda表达式,但我从来没有觉得使用C#的代表组播方面,即我从来没有想多与会代表结合在一起在一个委托的冲动。因此,我很好奇在什么样的情况下多路广播委托的是有用的 - 我只能想到的例子中,你可以很容易地实现该功能是以其他方式,比方说,链代表,或将它们放在一个列表。 P>
在特定的,埃里克利珀的回答<一href="http://stackoverflow.com/questions/2306814/co-and-contravariance-bugs-in-net-4-0/2307942#2307942">here给人的IM pression甚至C#团队有时会忘掉代表的multicastiness。
任何作为一个事件是经典的答案在这里 - 那么调用者并不需要知道谁是听(只是调用它,如果它是非空值)。这是一个理想的多个操作可以同时订阅 - 例如3个独立的控制观察(数据绑定)到一个视图模型相同的属性。
在任何情况下,委托充当功能(特别是与一个返回值)是特里克,因为你需要考虑你将如何处理的 - 拿第一?持续?总结?
When is it useful to have multicast delegates over singlecast delegates?
I use delegates a lot, mainly coupled with C# lambdas, but I've never felt the urge to use the multicast aspect of C# delegates, ie I've never wanted to combine multiple delegates together in a single delegate. I'm therefore very curious in what sort of situation multicast delegates are useful - I can only think of examples in which you can easily implement the functionality some other way, by, say, chaining the delegates or putting them in a list.
In particular, Eric Lippert's answer here gives the impression even the C# team sometimes forgets about the multicastiness of delegates.
Anything acting as an event is the classic answer here - then the caller doesn't need to know who is listening (just invoke it, if it is non-null). This is ideal as multiple operations can subscribe simultaneously - for example 3 separate controls observing (data-binding) to the same property on a view-model.
Any cases where the delegate is acting as function (in particular with a return value) is tricker, as you need to think how you will handle that - take the first? Last? Aggregate?
这篇关于在C#中多路广播委托使用组播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!