简体   繁体   English

多播代表的返回类型必须为void。 为什么?

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

Multicast Delegates must have a return type of void Otherwise it will throw an exception. 多播代表的返回类型必须为void,否则将引发异常。

I want to know whats the reason behind it, what if multiple methods could have a same return type as of a delegate ? 我想知道其背后的原因是什么,如果多个方法可能具有与委托相同的返回类型,该怎么办?

The premise is wrong; 前提是错误的; it works fine: 它工作正常:

Func<int> func = delegate { Console.WriteLine("first part"); return 5; };
func += delegate { Console.WriteLine("second part"); return 7; };
int result = func();

That is a multicast delegate with a non-void result, working fine. 那是一个没有无效结果的多播委托,可以正常工作。 You can see from the console that both parts executed. 您可以从控制台中看到两个部分均已执行。 The result of the last item is the one returned. 最后一项的结果是返回的结果。 We can demonstrate that this is a true multicast delegate: 我们可以证明这是一个真正的多播委托:

if(func is MulticastDelegate) Console.WriteLine("I'm multicast");

and it will write "I'm multicast" even after just the first line (when there is only a single method listed). 并且即使在第一行之后 (当仅列出一个方法时),它也会写“我正在多播”。

If you need more control over individual results, then use GetInvocationList() : 如果您需要对单个结果进行更多控制,请使用GetInvocationList()

foreach (Func<int> part in func.GetInvocationList())
{
    int result = part();
}

which allows you to see each individual result. 您可以查看每个结果。

In IL terminology: 在IL术语中:

.class public auto ansi sealed Func<+ TResult>
    extends System.MulticastDelegate`

which is to say: Func<T> inherits from MulticastDelegate . 也就是说: Func<T>继承自MulticastDelegate Basically, to all intents and purposes, all delegates in .NET are multicast delegates. 基本上,就所有意图和目的而言,.NET中的所有委托都是多播委托。 You might be able to get a non-multicast delegate in managed C++, I don't know. 我不知道,您也许可以在托管C ++中获得非多播委托。 But certainly not from C#. 但是肯定不是来自C#。

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?) 以调用顺序返回第一个方法的值(但是未指定IIRC调用顺序,那么它将如何工作?)
  • 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 如果不是所有人都同意,则抛出异常

in multicast the problem is that it override all values just print the last method value if it have return type,so you have to capture the return type of one by one,lets see the code below 在多播中,问题在于它覆盖了所有值,如果具有返回类型,则仅打印最后一个方法值,因此您必须一个接一个地捕获返回类型,请参见下面的代码

 class Program
{
    // i am going to add and subtract two num but i wanna get result in string the same thing you can do for int and what ever you want
      delegate string mydeledagte(int a,int b);
      delegate string d(int s, int t);
    static void Main(string[] args)
    {
        mydeledagte ab = new mydeledagte(ad);
        mydeledagte d= new mydeledagte(sub);
        mydeledagte multi = ab + d;

        foreach (mydeledagte individualMI in multi.GetInvocationList())
        {
            string retVal = individualMI(3, 5);
            Console.WriteLine("Output: " + retVal);
            Console.WriteLine("\n***developer of KUST***");
            Console.ReadKey();
        }
    }
    static string ad(int a, int b)
    {

        return (a + b).ToString();

    }
    static string sub(int a, int b)
    {

        return (a - b).ToString(); ;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM