Multicast Delegates must have a return type of void. Why?

前端 未结 3 1624
猫巷女王i
猫巷女王i 2021-01-05 03:22

Multicast Delegates must have a return type of void Otherwise it will throw an exception.

I want to know whats the reason behind it, what if multiple methods could h

3条回答
  •  情歌与酒
    2021-01-05 03:32

    The following answer is factually wrong, because you currently *can* have multicast delegates with non-void return type (the jury is still out regarding whether this has always been so). However, it does answer the question "Why might a language disallow such delegates?", so I am leaving it for completeness.

    Now go and upvote Marc.


    Because the multiple methods would return multiple values, so what should the one return value of the delegate be then? Clearly there is no answer that would be satisfying in all circumstances. You could argue that the multicast delegate should:

    • return the value of the first method in invocation order (but IIRC invocation order is unspecified, so how would this work?)
    • return the value of the last method, as above
    • return the single distinct value returned by all delegates; throw an exception if not all of them agree

提交回复
热议问题